如何控制Java中的线程,总结了3种方法...

问题:利用Java多线程,轮流打印数字,也就是怎么控制线程....

1:通过synchronized的关键字,对类的static final 成员进行Lock,锁住对象,来实现同步。

    private int id;
    private static int n = 0;
    private static final ThreadTest lock = new ThreadTest();

    public ThreadTest(int id) {
        this.id = id;
    }
    public ThreadTest () {

    }
    @Override
    public void run() {
        while (n < 100) {
            synchronized (lock) {
                while ((n % 5 == 0) && (n / 5) % 5 != id) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (n < 100) {
                    System.out.print("Thread-" + (id+1) + " : " + " " + (n + 1)
                            + " " + (n + 2) + " " + (n + 3) + " " + (n + 4)
                            + " " + (n + 5) + "\n");
                    n += 5;
                }
                lock.notifyAll();
            }
        }
    }


2.通过AtomicInteger对象,和ExecutorService实现线程之间的同步

 

    private AtomicInteger atomicInteger=new AtomicInteger(0);
    private static final int max=20;

    class Thread1 implements Runnable{
        private int mark=0;
        public Thread1(int i){
            this.mark=i;
        }            
        public void run() {
            while(atomicInteger.get()<max){
                if(atomicInteger.get()%5==mark){
                    System.out.println("线程Thread"+(mark+1)+"打印:"+(atomicInteger.get()*5+1)+" "
                                    +(atomicInteger.get()*5+2)+" "+(atomicInteger.get()*5+3)+" "
                                    +(atomicInteger.get()*5+4)+" "+(atomicInteger.get()*5+5));
                    atomicInteger.getAndIncrement();
                }
            }
        }
    }

3.通过ReentrantLock对象和Condition对象实现线程之间的同步

private  int state = 1;
    private  int n = 1;    
    private ReentrantLock lock=new ReentrantLock();
    private Condition condition1=lock.newCondition();
    private Condition condition2=lock.newCondition();
    private Condition condition3=lock.newCondition();
    @Override
    public void run(){
        new Thread(new Runnable() {            
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 5; i++) {
                    try {
                        lock.lock();
                        while(state!=1)
                            try{
                                condition1.await();
                            }
                        catch (InterruptedException e) {
                            // TODO: handle exception
                            e.printStackTrace();
                        }
                        System.out.print(Thread.currentThread().getName()+": ");
                        for (int j = 0; j < 5; j++) {
                            System.out.print(n+++" ");
                        }
                        System.out.println();    
                        state=2;
                        condition2.signal();
                    } finally{
                        lock.unlock();
                    }
                }
            }
        },"线程1").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 5; i++) {
                    try{
                        lock.lock();
                        while(state!=2)
                            try {
                                condition2.await();
                            } catch (InterruptedException e) {
                                // TODO: handle exception
                                e.printStackTrace();
                            }
                        System.out.print(Thread.currentThread().getName()+": ");
                        for (int j = 0; j < 5; j++) {
                            System.out.print(n+++" ");
                        }
                        System.out.println();    
                        state=3;
                        condition3.signal();
                    }
                    finally{
                        lock.unlock();
                    }
                }
            }
        },"线程2").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 5; i++) {
                    try{
                        lock.lock();
                        while(state!=3)
                            try {
                                condition3.await();
                            } catch (InterruptedException e) {
                                // TODO: handle exception
                                e.printStackTrace();
                            }
                        System.out.print(Thread.currentThread().getName()+": ");
                        for (int j = 0; j < 5; j++) {
                            System.out.print(n+++" ");
                        }
                        System.out.println();    
                        state=1;
                        condition1.signal();
                    }finally{
                        lock.unlock();
                    }
                }
            }
        },"线程3").start();
    }

 

posted @ 2015-08-13 15:04  toto怎么会喝醉  阅读(325)  评论(0编辑  收藏  举报