zoukankan      html  css  js  c++  java
  • 线程请求其他线程资源

      可以利用join方法实现这种需求另外一个线程的方法

     1 /*
     2      打酱油
     3          也就是一个线程完成它的任务,需要附加另一个线程的资源.
     4              利用join()方法实现资源的加入
     5  */
     6 
     7 class Mon extends Thread {
     8     
     9     @Override
    10     public void run() {
    11         System.out.println("发现没有酱油了,需要去买酱油");
    12         Son son = new Son();
    13         son.start();
    14         try {
    15             son.join();//利用join()方法实现,即加入资源的意思
    16         } catch (InterruptedException e) {
    17             // TODO Auto-generated catch block
    18             e.printStackTrace();
    19         }
    20         System.out.println("母亲拿到儿子的酱油");
    21         System.out.println("母亲做好菜一起吃饭");
    22     }
    23     
    24 }
    25 
    26 class Son extends Thread{
    27     
    28     @Override
    29     public void run() {
    30         System.out.println("儿子下楼");
    31         System.out.println("去酱油的路上,打到酱油");
    32         System.out.println("儿子上楼把酱油给母亲");
    33     }
    34 }
    35 
    36 public class Demo13 {
    37     
    38     public static void main(String[] args) {
    39         Mon mon = new Mon();
    40         mon.start();
    41     }
    42 }
  • 相关阅读:
    CSS样式2
    页面布局
    CSS样式1
    HTML
    Document
    Document
    Document
    Document
    Document
    Document
  • 原文地址:https://www.cnblogs.com/bequt/p/5656399.html
Copyright © 2011-2022 走看看