Discuss / Java / 加入一点打印语句,方便理解

加入一点打印语句,方便理解

Topic source

代码块:

public class DemoMainClassWaitNotify {

    public static void main(String[] args) throws InterruptedException {
        TaskQueen q=new TaskQueen();        List<Thread> rList=new ArrayList<Thread>();        for (int i = 0; i <5 ; i++) {
            var getT=new Thread("threadGet"+i){
                @Override                public void run() {
                    while (true){
                        try {
                            String s= q.getTask();                            System.out.println(Thread.currentThread().getName()+"线程-获取的东东是:"+s);                        } catch (InterruptedException e) {
                            System.out.println(Thread.currentThread().getName()+"线程被阻断,执行结束……");                            return;//被阻断就结束该线程                        }
                    }
                }
            };            getT.start();            rList.add(getT);        }

        var add=new Thread("athread"){
            @Override            public void run() {
                for (int i = 0; i < 10; i++) {
                    String s="add-"+Math.random();                    q.addTask(s);                }
            }
        };        add.start();        add.join();        Thread.sleep(100);        for (int i = 0; i <rList.size() ; i++) {
            rList.get(i).interrupt();        }
    }

}
class TaskQueen {
    Queue<String> s=new LinkedList();    public synchronized String getTask() throws InterruptedException {
        while (s.isEmpty()){
            System.out.println("线程进入等待:"+Thread.currentThread().getName());            this.wait();            System.out.println("线程被唤醒之后:"+Thread.currentThread().getName());        }
        System.out.println("getTask-当前正在消费的线程:"+Thread.currentThread().getName());        return s.remove();    }

    public synchronized void addTask(String a){
        s.add(a);        System.out.println("addTask-正在加加加,当前线程是:"+Thread.currentThread().getName());        this.notifyAll();//唤醒这个锁的所有等待的对象    }
}

执行过程:

线程进入等待:threadGet0

线程进入等待:threadGet2

线程进入等待:threadGet4

线程进入等待:threadGet3

线程进入等待:threadGet1

addTask-正在加加加,当前线程是:athread

线程被唤醒之后:threadGet0

getTask-当前正在消费的线程:threadGet0

addTask-正在加加加,当前线程是:athread

线程被唤醒之后:threadGet1

getTask-当前正在消费的线程:threadGet1

线程被唤醒之后:threadGet3

线程进入等待:threadGet3

线程被唤醒之后:threadGet4

线程进入等待:threadGet4

线程被唤醒之后:threadGet2

线程进入等待:threadGet2

addTask-正在加加加,当前线程是:athread

线程被唤醒之后:threadGet3

getTask-当前正在消费的线程:threadGet3

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

addTask-正在加加加,当前线程是:athread

线程被唤醒之后:threadGet2

getTask-当前正在消费的线程:threadGet2

线程被唤醒之后:threadGet4

getTask-当前正在消费的线程:threadGet4

threadGet0线程-获取的东东是:add-0.49987982335784675

threadGet3线程-获取的东东是:add-0.29978176800017

threadGet4线程-获取的东东是:add-0.034695131303512095

threadGet1线程-获取的东东是:add-0.15791777476651536

threadGet2线程-获取的东东是:add-0.5238940542039172

getTask-当前正在消费的线程:threadGet0

threadGet0线程-获取的东东是:add-0.8834450167132556

getTask-当前正在消费的线程:threadGet2

getTask-当前正在消费的线程:threadGet1

threadGet2线程-获取的东东是:add-0.8827609040916883

getTask-当前正在消费的线程:threadGet4

threadGet1线程-获取的东东是:add-0.2528793630549284

getTask-当前正在消费的线程:threadGet3

threadGet4线程-获取的东东是:add-0.5962494801730128

threadGet3线程-获取的东东是:add-0.309328001632865

线程进入等待:threadGet1

线程进入等待:threadGet2

线程进入等待:threadGet0

线程进入等待:threadGet3

线程进入等待:threadGet4

threadGet2线程被阻断,执行结束……

threadGet4线程被阻断,执行结束……

threadGet3线程被阻断,执行结束……

threadGet1线程被阻断,执行结束……

threadGet0线程被阻断,执行结束……

乱掉了,重新发一次代码块

package com.zoe.demo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class DemoMainClassWaitNotify {

    public static void main(String[] args) throws InterruptedException {
        TaskQueen q=new TaskQueen();
        List<Thread> rList=new ArrayList<Thread>();
        for (int i = 0; i <5 ; i++) {
            var getT=new Thread("threadGet"+i){
                @Override
                public void run() {
                    while (true){
                        try {
                            String s= q.getTask();
                            System.out.println(Thread.currentThread().getName()+"线程-获取的东东是:"+s);
                        } catch (InterruptedException e) {
                            System.out.println(Thread.currentThread().getName()+"线程被阻断,执行结束……");
                            return;//被阻断就结束该线程
                        }
                    }
                }
            };
            getT.start();
            rList.add(getT);
        }

        var add=new Thread("athread"){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    String s="add-"+Math.random();
                    q.addTask(s);
                }
            }
        };

        add.start();
        add.join();
        Thread.sleep(100);
        for (int i = 0; i <rList.size() ; i++) {
            rList.get(i).interrupt();
        }
    }

}
class TaskQueen {
    Queue<String> s=new LinkedList();
    public synchronized String getTask() throws InterruptedException {
        while (s.isEmpty()){
            System.out.println("线程进入等待:"+Thread.currentThread().getName());
            this.wait();
            System.out.println("线程被唤醒之后:"+Thread.currentThread().getName());
        }
        System.out.println("getTask-当前正在消费的线程:"+Thread.currentThread().getName());
        return s.remove();
    }

    public synchronized void addTask(String a){
        s.add(a);
        System.out.println("addTask-正在加加加,当前线程是:"+Thread.currentThread().getName());
        this.notifyAll();//唤醒这个锁的所有等待的对象
    }
}



  • 1

Reply