zoukankan      html  css  js  c++  java
  • 创建三个线程按顺序输出1-60,每个线程输出5个数

    创建三个线程按顺序输出1-60,每个线程输出5个数

    public class ThreadStart {
    public static void main(String[] args) {
    Thread t1 = new ThreadA();
    Thread t2 = new ThreadB();
    Thread t3 = new ThreadC();
    t1.start();
    t2.start();
    t3.start();
    }
    }
    class ThreadA extends Thread {
    @Override
    public void run() {
    super.run();
    for (int i =1; i <=4 ; i++) {
    try {
    Thread.sleep(95);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.print("A: ");
    for(int j=15*(i-1)+1;j<(15*(i-1)+6);j++){
         if(j!=15*(i-1)+5)
                System.out.print(j+" ");
              else System.out.print(j);
    }
    System.out.println("");
    }
    }
    }
    class ThreadB extends Thread {
    @Override
    public void run() {
    super.run();
    for (int i =1; i <=4 ; i++) {
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.print("B: ");
    for(int j=15*(i-1)+6;j<15*(i-1)+11;j++){
         if(j!=15*(i-1)+10)
                System.out.print(j+" ");
              else System.out.print(j);
    }
    System.out.println("");
    }
    }
    }
    class ThreadC extends Thread {
    @Override
    public void run() {
    super.run();
    for (int i =1; i <=4 ; i++) {
    try {
    Thread.sleep(105);
    } catch (InterruptedException e) {
                       e.printStackTrace();
    }
    System.out.print("C: ");
    for(int j=15*(i-1)+11;j<15*i+1;j++){
      if(j!=15*i)
        System.out.print(j+" ");
      else System.out.print(j);
    }
    System.out.println("");
    }
    }
    }


     2019-11-05更新简便方法,之前写的太啰嗦了

    方法一 利用synchronized:同一时刻,只有一个线程可以执行某个方法或某个代码块

    public class Main01  {
    
        public static void main(String[] args) throws Exception {
    
            ProcessingThread pt = new ProcessingThread();
            Thread t1 = new Thread(pt,"A");
            Thread t2 = new Thread(pt,"B");
            Thread t3 = new Thread(pt,"C");
            t1.start();
            t2.start();
            t3.start();
    
    
    
        }
    }
    class ProcessingThread implements Runnable {
          private int count;
    
        @Override
        public void run() {
            for (int i = 1; i < 5; i++) {
                processSomething(i);
                synchronized(this){
                    System.out.print(Thread.currentThread().getName()+"|");
                    for(int c=0;c<4;c++){
                        System.out.print(++count+",");
                    }
                    System.out.println(++count);
                }
            }
        }
    
    
        public synchronized int getCount() {
    //        return count.get();
            return count;
        }
    
    
        private void processSomething(int i) {
            // processing some job
            try {
                Thread.sleep(i * 800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }

    利用AtomicInteger原子操作

    import java.util.concurrent.atomic.AtomicInteger;
    
    public class Main01  {
    
        public static void main(String[] args) throws Exception {
    
            ProcessingThread pt = new ProcessingThread();
            Thread t1 = new Thread(pt,"A");
            Thread t2 = new Thread(pt,"B");
            Thread t3 = new Thread(pt,"C");
            t1.start();
            t2.start();
            t3.start();
    
    
    
        }
    }
    class ProcessingThread implements Runnable {
        private AtomicInteger count =new AtomicInteger();
    
        @Override
        public void run() {
            for (int i = 1; i < 5; i++) {
                processSomething(i);
                synchronized(this){
                    System.out.print(Thread.currentThread().getName()+"|");
                    for(int c=0;c<4;c++){
                        System.out.print(count.incrementAndGet()+",");
                    }
                    System.out.println(count.incrementAndGet());
                }
            }
        }
    
    
        public synchronized int getCount() {
            return count.get();
        }
    
    
        private void processSomething(int i) {
            try {
                Thread.sleep(i * 800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }

    结果

  • 相关阅读:
    VIM 用正则表达式,非贪婪匹配,匹配竖杠,竖线, 匹配中文,中文正则,倒数第二列, 匹配任意一个字符 :
    中国科学院图书馆分类法
    让进程在后台可靠运行的几种方法 nohup,setsid,&,disown,CTRL-z ,screen
    Exception Handling Statements (C# Reference)
    ChannelFactory.Endpoint 上的地址属性为空。ChannelFactory 的终结点必须指定一个有效的地址。
    .NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱
    WCF Host中的BaseAddress 和 Endpoint中的Address的区别
    使用vs自带的wcf配置工具
    Automatic Code Generation-->Implement Interface
    Learning WCF Chapter1 Exposing Multiple Service Endpoints
  • 原文地址:https://www.cnblogs.com/tk55/p/6580307.html
Copyright © 2011-2022 走看看