• 一个线程同步的例子..


     1 class Info {
     2      
     3     public String getName() {
     4         return name;
     5     }
     6  
     7     public void setName(String name) {
     8         this.name = name;
     9     }
    10  
    11     public int getAge() {
    12         return age;
    13     }
    14  
    15     public void setAge(int age) {
    16         this.age = age;
    17     }
    18  
    19     public synchronized void set(String name, int age){
    20         this.name=name;
    21         try{
    22             Thread.sleep(100);
    23         }catch (Exception e) {
    24             e.printStackTrace();
    25         }
    26         this.age=age;
    27     }
    28      
    29     public synchronized void get(){
    30         try{
    31             Thread.sleep(100);
    32         }catch (Exception e) {
    33             e.printStackTrace();
    34         }
    35         System.out.println(this.getName()+"<===>"+this.getAge());
    36     }
    37     private String name = "Rollen";
    38     private int age = 20;
    39 }
    40  
    41 /**
    42  * 生产者
    43  * */
    44 class Producer implements Runnable {
    45     private Info info = null;
    46  
    47     Producer(Info info) {
    48         this.info = info;
    49     }
    50  
    51     public void run() {
    52         boolean flag = false;
    53         for (int i = 0; i < 25; ++i) {
    54             if (flag) {
    55                  
    56                 this.info.set("Rollen", 20);
    57                 flag = false;
    58             } else {
    59                 this.info.set("ChunGe", 100);
    60                 flag = true;
    61             }
    62         }
    63     }
    64 }
    65  
    66 /**
    67  * 消费者类
    68  * */
    69 class Consumer implements Runnable {
    70     private Info info = null;
    71  
    72     public Consumer(Info info) {
    73         this.info = info;
    74     }
    75  
    76     public void run() {
    77         for (int i = 0; i < 25; ++i) {
    78             try {
    79                 Thread.sleep(100);
    80             } catch (Exception e) {
    81                 e.printStackTrace();
    82             }
    83             this.info.get();
    84         }
    85     }
    86 }
    87  
    88 /**
    89  * 测试类
    90  * */
    91 class hello {
    92     public static void main(String[] args) {
    93         Info info = new Info();
    94         Producer pro = new Producer(info);
    95         Consumer con = new Consumer(info);
    96         new Thread(pro).start();
    97         new Thread(con).start();
    98     }
    99 }
  • 相关阅读:
    Python之运算符
    Day1_Python基础_10..pyc是个什么鬼?
    Day1_Python基础_9.模块初识
    Day1_Python基础_8.用户输入
    Day1_Python基础_7.字符编码
    Day1_Python基础_6.变量/字符编码
    Day1_Python基础_5.Hello World 程序
    Day1_Python基础_4.Python安装
    Day1_Python基础_3.Python2 or 3 ?
    Day1_Python基础_2.Python历史
  • 原文地址:https://www.cnblogs.com/Akishimo/p/2964549.html
Copyright © 2020-2023  润新知