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

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

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

    brand new start

    常用鏈接

    統計

    最新評論

    2012年6月14日 #

    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 @ 2012-06-14 17:12 Nemo_blinker 閱讀(1027) | 評論 (0)編輯 收藏

    僅列出標題  下一頁
    主站蜘蛛池模板: 亚洲天堂中文字幕在线观看| 亚洲中文字幕伊人久久无码| 久久综合日韩亚洲精品色| 一级做a爱过程免费视| 日韩午夜免费视频| 亚洲AV无码专区在线电影成人 | 亚洲综合精品伊人久久| 国产精彩免费视频| 亚洲天堂电影在线观看| 欧洲一级毛片免费| 亚洲毛片基地4455ww| 成人免费AA片在线观看| 日本亚洲免费无线码| 日韩毛片无码永久免费看| 亚洲精品GV天堂无码男同| 免费视频中文字幕| 久久精品国产亚洲AV| 国产一区二区三区免费看| 免费无遮挡无遮羞在线看| 国产成人亚洲综合| 国产免费一区二区视频| 亚洲精品自产拍在线观看动漫| 人妻无码一区二区三区免费| 91亚洲国产成人久久精品网址| 18禁免费无码无遮挡不卡网站 | 久久亚洲AV成人无码软件| 24小时免费看片| 2017亚洲男人天堂一| 国产gav成人免费播放视频| kk4kk免费视频毛片| 亚洲av丰满熟妇在线播放| 国产又大又粗又长免费视频| 亚洲精品无码aⅴ中文字幕蜜桃| 日韩视频在线免费| 久久毛片免费看一区二区三区| 亚洲视频在线观看免费| 香蕉视频在线观看免费国产婷婷 | 久久久久国产精品免费网站| 亚洲人成网站在线观看播放青青| 日本成人免费在线| 国产午夜成人免费看片无遮挡 |