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();
            }
        }
    
    }

    结果

  • 相关阅读:
    SAP ABAP 开发中常用的方法总结
    安装sap帮助文档
    spring成神之路第七篇:DI依赖注入之手动注入
    spring成神之路第三篇:Spring容器基本使用及原理(ApplicationContext接口获取bean的4种方式)
    spring成神之路第九篇:dependon到底是干什么的?
    spring成神之路第十篇:primary可以解决什么问题?
    spring成神之路第四篇:xml中bean定义详解
    spring成神之路第六篇:玩转bean scope,避免跳坑里
    spring成神之路第一篇:为何要学spring
    spring成神之路第五篇:创建bean实例的方式
  • 原文地址:https://www.cnblogs.com/tk55/p/6580307.html
Copyright © 2011-2022 走看看