<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 閱讀(1023) | 評論 (0)編輯 收藏

    主站蜘蛛池模板: 美女视频黄的免费视频网页| 又大又硬又粗又黄的视频免费看| 亚洲av丰满熟妇在线播放| 久久亚洲精品成人综合| 思思99re66在线精品免费观看| 一级**爱片免费视频| 久久久影院亚洲精品| 亚洲色大成网站www永久网站| 特级无码毛片免费视频| 亚洲毛片免费观看| 免费成人在线观看| 美女视频黄a视频全免费| eeuss影院免费92242部| 最近中文字幕mv手机免费高清 | 亚洲午夜精品一级在线播放放| 拨牐拨牐x8免费| 久久精品无码精品免费专区| 久久福利资源网站免费看| 好男人视频社区精品免费| 国产亚洲大尺度无码无码专线| 亚洲午夜在线一区| 免费看片A级毛片免费看| 午夜影院免费观看| 午夜免费福利在线观看| 亚洲欧洲∨国产一区二区三区| 成人免费视频小说| 亚洲色精品aⅴ一区区三区| 久久久久久亚洲av成人无码国产 | 免费国产成人高清在线观看网站| 国产一区二区三区免费观在线| 国产视频精品免费视频| 国产美女无遮挡免费视频网站 | 亚洲av无码片vr一区二区三区| 24小时在线免费视频| 国产午夜无码精品免费看| 黄页网站在线看免费| 久久亚洲AV无码精品色午夜麻豆| 一级毛片免费播放视频| 九九综合VA免费看| 青苹果乐园免费高清在线| 永久免费毛片在线播放|