美文网首页
多线程10wait和notify

多线程10wait和notify

作者: RyanHugo | 来源:发表于2020-03-22 15:04 被阅读0次

    代码

    import com.roocon.thread.t5.Singleton2;
    //一个线程执行完后,更改状态,再执行下一个
    public class Demo2 {
       
       private volatile int signal;
       
       public void set (int value) {
           this.signal = value;
       }
       
       public int get () {
           return signal;
       }
       
       public static void main(String[] args) {
           Demo2 d = new Demo2();
           new Thread(new Runnable() {
               
               @Override
               public void run() {
                   synchronized (d) {
                       System.out.println("修改状态的线程执行...");
                       try {
                           Thread.sleep(5000);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       d.set(1);
                       System.out.println("状态值修改成功。。。");
                       d.notify();
                   }
               }
           }).start();
           
           
           new Thread(new Runnable() {
               
               @Override
               public void run() {
                   synchronized (d) {
                       // 等待signal为1开始执行,否则不能执行
                       while(d.get() != 1) {
                           try {
                               d.wait();
                           } catch (InterruptedException e) {
                               e.printStackTrace();
                           }
                       }
                       // 当信号为1 的时候,执行代码
                       System.out.println("模拟代码的执行...");
                   }
               }
           }).start();
       }
    
    }
    

    示例

    public class Target1 implements Runnable {
        
        private Demo3 demo;
        
        public Target1(Demo3 demo) {
            this.demo = demo;
        }
    
        @Override
        public void run() {
            demo.set();
        }
    
    }
    public class Target2 implements Runnable {
        
        private Demo3 demo;
    
        public Target2(Demo3 demo) {
            this.demo = demo;
        }
    
        @Override
        public void run() {
            demo.get();
        }
    
    }
    
    
    import java.util.concurrent.TimeUnit;
    /*
     * 1.wait,notify必须要在同步代码块执行,synchronized
     * 2.加对象d.wait()   d.notify()   见demo类
     * 3.调用wait会使线程失去锁(synchronized)
     * 4.notify执行后需要等锁释放才会唤醒别的线程
     */
            
    public class Demo3 {
    
    private volatile int signal;
        
        public synchronized void set () {
            signal = 1;
            notifyAll(); // notify方法会随机叫醒一个处于wait状态的线程
             // notifyAll叫醒所有的处于wait线程,争夺到时间片的线程只有一个
            System.out.println("叫醒线程叫醒之后休眠开始...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        public synchronized int get () {
            System.out.println(Thread.currentThread().getName() + " 方法执行了...");
            if(signal != 1) {
                try {
                    wait();
                    System.out.println("叫醒之后");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + " 方法执行完毕...");
            return signal;
        }
        
        public static void main(String[] args) {
            
            Demo3 d = new Demo3();
            Target1 t1 = new Target1(d);
            Target2 t2 = new Target2(d);
            
            new Thread(t2).start();
            new Thread(t2).start();
            new Thread(t2).start();
            new Thread(t2).start();
            
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            
            new Thread(t1).start();
            
        }
    }
    

    相关文章

      网友评论

          本文标题:多线程10wait和notify

          本文链接:https://www.haomeiwen.com/subject/kfcvyhtx.html