面试问题:线程控制和线程之间的通信

时间:2021-03-27 17:32:56

一、面试碰到过这样的问题:

     让两个多线程依次打印奇数和偶数。

二、问题解释:

     大家都知道,线程一旦启动,CPU有一定的调度策略,不是人为能随便控制的。这样的题目在实际工程中也没有多大的意义,不用多线程,直接用方法依次调用就可以了,但为什么还要考这样的题目,实际上是考对多线程和线程之间的通信相关知识点。现在总结如下:

 

三、线程控制和线程之间的通信

   主要是三个方法wait(); notify(); notifyAll();

   1、wait()、notify/notifyAll() 方法是Object的本地final方法,无法被重写。

   2、wait()使当前线程阻塞,前提是 必须先获得锁,一般配合synchronized 关键字使用,即,一般在synchronized 同步代码块里使用 wait()、notify/notifyAll() 方法。
   3、 由于 wait()、notify/notifyAll() 在synchronized 代码块执行,说明当前线程一定是获取了锁的。
       当线程执行wait()方法时候,会释放当前的锁,然后让出CPU,进入等待状态。
       只有当 notify/notifyAll() 被执行时候,才会唤醒一个或多个正处于等待状态的线程,然后继续往下执行,直到执行完synchronized 代码块的代码或是中途遇到wait() ,再次释放锁。
       也就是说,notify/notifyAll() 的执行只是唤醒沉睡的线程,而不会立即释放锁,锁的释放要看代码块的具体执行情况。所以在编程中,尽量在使用了notify/notifyAll() 后立即退出临界区,以唤醒其他线程 
   4、wait() 需要被try catch包围,中断也可以使wait等待的线程唤醒。
   5、notify 和wait 的顺序不能错,如果A线程先执行notify方法,B线程在执行wait方法,那么B线程是无法被唤醒的。
   6、notify 和 notifyAll的区别
       notify方法只唤醒一个等待(对象的)线程并使该线程开始执行。所以如果有多个线程等待一个对象,这个方法只会唤醒其中一个线程,选择哪个线程取决于操作系统对多线程管理的实现。notifyAll 会唤醒所有等待(对象的)线程,尽管哪一个线程将会第一个处理取决于操作系统的实现。如果当前情况下有多个线程需要被唤醒,推荐使用notifyAll 方法。比如在生产者-消费者里面的使用,每次都需要唤醒所有的消费者或是生产者,以判断程序是否可以继续往下执行。
  7、wait(), notify(), notifyAll() 必须是同一对象的多个线程之间的通信
 
四、面试题举例
     两个线程,依次执行打印奇数和偶数。
    
     因为wait() notifyAll() 必须是同一对象的多个线程之间的通信,所以有如下程序:
     
 1 public class Printer {
 2     
 3     int i = 1;
 4 
 5     synchronized public void  printEven(){
 6         for(;i<=100;) {
 7             if (i % 2 == 1) {
 8                 try {
 9                     wait();
10                 } catch (InterruptedException e) {
11                     e.printStackTrace();
12                 }
13             } else {
14                 System.out.println(Thread.currentThread().getName() + " is printting i: " + i++);
15                 notifyAll();
16             }
17         }
18     }
19 
20     synchronized public void printOdd(){
21         for(; i<=100; ) {
22             if (i % 2 == 0) {
23                 try {
24                     wait();
25                 } catch (InterruptedException e) {
26                     e.printStackTrace();
27                 }
28             } else {
29                 System.out.println(Thread.currentThread().getName() + " is printting i: " + i++);
30                 notifyAll();
31             }
32         }
33     }
34 
35 }

 

要实现两个线程分别打印,所以有如下代码:

 1 public class PrintEven implements Runnable{
 2     Printer printer;
 3     public PrintEven(Printer printer){
 4         this.printer = printer;
 5     }
 6     public void run() {
 7         printer.printEven();
 8 
 9     }
10 }
 1 public class PrintOdd implements Runnable {
 2     Printer printer;
 3     public PrintOdd(Printer printer){
 4         this.printer = printer;
 5     }
 6     public void run() {
 7         printer.printOdd();
 8 
 9     }
10 }

 

主函数: 把printer作为对象传入

 1     public  static void main(String[] args){
 2 
 3         Printer printer = new Printer();
 4 
 5         Runnable printOdd = new PrintOdd(printer);
 6         Thread threadOdd = new Thread(printOdd, "thread1");
 7 
 8         Runnable printEven = new PrintEven(printer);
 9         Thread threadEven = new Thread(printEven, "thread2");
10 
11         threadOdd.start();
12         threadEven.start();
13     }

程序运行结果:

 1 thread1 is printting i: 1
 2 thread2 is printting i: 2
 3 thread1 is printting i: 3
 4 thread2 is printting i: 4
 5 thread1 is printting i: 5
 6 thread2 is printting i: 6
 7 thread1 is printting i: 7
 8 thread2 is printting i: 8
 9 thread1 is printting i: 9
10 thread2 is printting i: 10
11 thread1 is printting i: 11
12 thread2 is printting i: 12
13 thread1 is printting i: 13
14 thread2 is printting i: 14
15 thread1 is printting i: 15
16 thread2 is printting i: 16
17 thread1 is printting i: 17
18 thread2 is printting i: 18
19 thread1 is printting i: 19
20 thread2 is printting i: 20