首页 > 编程语言 > 详细

线程问题

时间:2015-11-23 00:54:53      阅读:331      评论:0      收藏:0      [点我收藏+]
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();
        
    }
    
}

 

线程问题

原文:http://www.cnblogs.com/freed0m/p/4987085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!