zoukankan      html  css  js  c++  java
  • 一个小小的java多线程

    对多线程方面一直只限概念,感觉用到的不多,所以没深入去了解。但发现面试时却经常会问到,于是便想了一个简单的题目,亲自实践下。题目如下: 
    由2个线程控制主线程的一个变量,一个调用加的方法,一个调用减的方法,要求变量值不能小于0(如果等于0,则减的方法必须等待)。一个典型的有货才卖的类型 ,由于新手,所以捣鼓了好久,终于成功了,下面是代码,不知道有什么能改进的。

    Java代码:  
    1. public class ThreadTest {  
    2.   
    3.     static Thread1 t1;  
    4.     static Thread2 t2;  
    5.     /** 
    6.      * @param args 
    7.      */  
    8.     public static void main(String[] args) {  
    9.         t2 = new Thread2();  
    10.         t1 = new Thread1(t2);  
    11.         t1.start();  
    12.         t2.start();  
    13.     }  
    14.       
    15.       
    16.     static int num = 0;  
    17.     public static void add() {  
    18.         num++;  
    19.         System.out.println("num+1="+num);  
    20.     }    
    21.     public synchronized static void minus() {  
    22.         if(num <= 0)  
    23.             try {  
    24.                 Thread.currentThread().wait();  
    25.             } catch (InterruptedException e) {  
    26.                 e.printStackTrace();  
    27.             }  
    28.         num--;  
    29.         System.out.println("num-1="+num);  
    30.     }  
    31. }   
    32. class Thread1 extends Thread {  
    33.     Thread2 t2;  
    34.     public Thread1(Thread2 t2){  
    35.         this.t2 = t2;  
    36.     }  
    37.     @SuppressWarnings("static-access")  
    38.     public void run() {  
    39.         int i = 100;  
    40.         while(i-->0) {  
    41.             // 休息100毫秒,以便更好的检查效果  
    42.             /*try { 
    43.                 this.sleep(100); 
    44.            } catch (InterruptedException e) { 
    45.                 e.printStackTrace(); 
    46.             }*/  
    47.             synchronized (ThreadTest.class) {  
    48.                 ThreadTest.add();  
    49.             }  
    50.             if(t2.interrupted())  
    51.             notifyAll();  
    52.         }  
    53.     }  
    54. }  
    55.   
    56. class Thread2 extends Thread {  
    57.       
    58.     public void notifyer() {  
    59.         this.notify();  
    60.     }  
    61.       
    62.     public void run() {  
    63.         int i = 100;  
    64.         while(i>0) {  
    65.             synchronized (ThreadTest.class) {  
    66.                 if(ThreadTest.num>0){  
    67.                     ThreadTest.minus();  
    68.                    i--;// 之前把i--写在while里了,老丢数据。  
    69.                 }  
    70.             }  
    71.         }  
    72.     }  
    73. }  


    回复1:提个建议,写线程不要直接继承Thread,去实现Runnable,除了更灵活,还能实现资源共享。
    回复2:你可以用AtomicLong/AtomicInteger: incrementAndGet()增加数量,在循环中用 get() 和 compareAndSet() 减少数量。 notify就在incrementAndGet以后,wait就在get()返回0以后。

     原文参考自:懒人之家www.lanrenzhijia.com    转载请注明

  • 相关阅读:
    TCP/IP断网仍然显示连通状态禁止“媒体感知”
    ASP.NET页面优化,提高载入速度方法大全
    国家气象局提供
    datatable&list<T>
    windows mobile C# net3.5 如何捕捉Arrow的CheckBox的KeyDown事件?
    hhclass 类 RAPI OpenNETCF.Desktop.Communication.DLL相关下载
    string和stringBuilder区别
    windows mobile全屏设置笔记
    CSS浮动(float,clear)通俗讲解
    js图片延时加载(jquery特效)
  • 原文地址:https://www.cnblogs.com/heatpress/p/3144728.html
Copyright © 2011-2022 走看看