<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    brand new start

    常用鏈接

    統計

    最新評論

    java 多線程

    生產消費例子:
    用synchronized:
    class Resource{
        
    private String name;
        
    private int count = 0;
        
    private boolean flag = true;
        
    public synchronized void set(String name){
            
    while(!flag){
                
    try {
                    wait();
                } 
    catch (InterruptedException e) {
                
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
    if(flag){
                
    this.name = name;
                System.out.println(Thread.currentThread().getName()
    +"   生產了    "+name+""+(++count));
                
            }
            flag 
    = false;
            notifyAll();
        }
        
    public synchronized void get(){
            
    while(flag){
                
    try {
                    wait();
                } 
    catch (InterruptedException e) {
                    
    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
    if(!flag){
                System.out.println(Thread.currentThread().getName()
    +"   消費了    "+name+""+count);
                
            }
            flag 
    = true;
            notifyAll();
        }
    }

    class Producer implements Runnable{
        
    private Resource r;
        Producer(Resource r){
            
    this.r = r;
        }
        
    public void run(){
            
    while(true){
                r.set(
    "Tom");
            }
        }
        
    }

    class Consumer implements Runnable{
        
    private Resource r;
        Consumer(Resource r){
            
    this.r = r;
        }
        
    public void run(){
            
    while(true){
                r.get();
            }
        }
        
    }

    public class TradeDemo {
        
    public static void main(String[] args){
            Resource res 
    = new Resource();
            Producer p 
    = new Producer(res);
            Consumer c 
    = new Consumer(res);
            Thread t1 
    = new Thread(p);
            Thread t2 
    = new Thread(p);
            Thread t3 
    = new Thread(c);
            Thread t4 
    = new Thread(c);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    用Lock:
    import java.util.concurrent.locks.*;

    import org.junit.Test;

    class Resources {
        
    private String name;
        
    private int count = 0;
        
    private boolean flag = true;
        
    private Lock lock = new ReentrantLock();
        
    private Condition condition_pro = lock.newCondition();
        
    private Condition condition_con = lock.newCondition();

        
    public void set(String name) {
            lock.lock();
            
    try {
                
    while (!flag) {
                    condition_pro.await();
                }
                
    if (flag) {
                    
    this.name = name;
                    System.out.println(Thread.currentThread().getName()
                            
    + "   生產了    " + name + "" + (++count));

                }
                flag 
    = false;
                condition_con.signal();
            } 
    catch (InterruptedException e) {
                System.out.println(
    "生產exception");
            } 
    finally {
                lock.unlock();
            }
        }

        
    public void get() {
            lock.lock();
            
    try {
                
    while (flag) {
                    condition_con.await();
                }
                
    if (!flag) {
                    System.out.println(Thread.currentThread().getName()
                            
    + "   消費了    " + name + "" + count);

                }
                flag 
    = true;
                condition_pro.signal();
            } 
    catch (InterruptedException e) {
                System.out.println(
    "消費exception");
            } 
    finally {
                lock.unlock();
            }
        }
    }

    class Producers implements Runnable {
        
    private Resources r;

        Producers(Resources r) {
            
    this.r = r;
        }

        
    public void run() {
            
    while (true) {
                r.set(
    "Tom");
            }
        }

    }

    class Consumers implements Runnable {
        
    private Resources r;

        Consumers(Resources r) {
            
    this.r = r;
        }
        @Test
        
    public void run() {
            
    while (true) {
                r.get();
            }
        }

    }

    public class TradeDemo2 {
        
    public static void main(String[] args) {
            Resources res 
    = new Resources();
            Producers p 
    = new Producers(res);
            Consumers c 
    = new Consumers(res);
            Thread t1 
    = new Thread(p);
            Thread t2 
    = new Thread(p);
            Thread t3 
    = new Thread(c);
            Thread t4 
    = new Thread(c);
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    }
    Dead lock:
    public class DeadLockThread implements Runnable{
        
    private int tickets = 100;
        
    private boolean flag;
        DeadLockThread(
    boolean flag){
            
    this.flag = flag;
        }
        
    public void run(){
            
    while(tickets>0){
                
    if(flag){
                    
    synchronized(MyLock.locka){
                        System.out.println(
    "if locka");
                        
    synchronized(MyLock.lockb){
                            System.out.println(Thread.currentThread().getName()
    +"..true.."+"if lockb  "+tickets--);
                        }
                    }
                }
                
    else{
                    
    synchronized(MyLock.lockb){
                        System.out.println(
    "else lockb");
                        
    synchronized(MyLock.locka){
                            System.out.println(Thread.currentThread().getName()
    +"..false.."+"else lockb  "+tickets--);
                        }
                    }
                }
            }
        }
        
    }

    class MyLock{
        
    static Object locka = new Object();
        
    static Object lockb = new Object();
    }

    posted on 2012-06-14 17:12 Nemo_blinker 閱讀(1023) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久久亚洲精品蜜桃臀| 日韩电影免费在线| 亚洲AV成人片色在线观看高潮| 男女男精品网站免费观看| 四虎成人精品在永久免费| 香港经典a毛片免费观看看| 国产一区视频在线免费观看 | 亚洲成AⅤ人影院在线观看| 国产亚洲精品美女2020久久| 国产免费人成在线视频| 一级毛片免费播放试看60分钟| 国产av无码专区亚洲国产精品| 久久WWW免费人成—看片| 亚洲综合网站色欲色欲| 国产免费网站看v片在线| 亚洲综合日韩中文字幕v在线| 6080午夜一级毛片免费看6080夜福利| 亚洲欧洲国产精品久久| 女人被男人躁的女爽免费视频| 国产亚洲精品国产福利在线观看| 国产成人毛片亚洲精品| 6080午夜一级毛片免费看6080夜福利| 亚洲性猛交xx乱| 四虎影永久在线高清免费| 在线观看人成视频免费无遮挡 | 国产v亚洲v天堂a无| 波多野结衣久久高清免费| 一级做a爰性色毛片免费| 亚洲日本精品一区二区| 午夜网站免费版在线观看| 一级美国片免费看| 亚洲视频中文字幕| 日本v片免费一区二区三区| 国产精品无码免费专区午夜| 97se亚洲综合在线| 波多野结衣免费视频观看| 永久免费av无码网站yy| 亚洲日韩看片无码电影| 亚洲精品乱码久久久久久按摩| 免费可以看黄的视频s色| a毛片成人免费全部播放|