线程问题

import java.util.concurrent.*;

public class ThreadTest4 {
    
    private static boolean samaphore = true;
    
    public static void main(String[] args) throws InterruptedException {
        final ThreadTest4 tt = new ThreadTest4();
        new Thread(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
                for(int i=0;i<5000;i++) {
                        try {
                            tt.subThread(i);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    
                }
            }
        }).start();
        
        for(int i=0;i<5000; i++) {
            tt.mainThread(i);
        }
    }

    private  synchronized void subThread(int jj) throws InterruptedException {
        if(!samaphore) {
            
            this.wait();
        }
            for(int i=0;i<10;i++) {
                System.out.println(jj+"---"+Thread.currentThread().getName()+"---"+i);
            }
            
            samaphore=false;
            this.notify();
    }
    
    private synchronized void mainThread(int jj) throws InterruptedException {
        if(samaphore) {
            this.wait();
        }
            for(int i=0;i<5;i++) {
                System.out.println(jj+"---"+Thread.currentThread().getName()+"---"+i);
            }
            
            samaphore=true;
            this.notify();
        
    }
    
}

 

posted on 2015-11-22 23:26  freedom's_blog  阅读(144)  评论(0编辑  收藏  举报

导航