• 如何控制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 += 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();
        }


    I am a slow walker, but I never walk backwards.



  • 相关阅读:
    Swift3 ——S3 API中间件兼容性测试
    解决 Python.h:没有那个文件或目录 错误的方法
    Swift云存储特性研究
    解决updateaptxapi占用资源过高的问题
    dll开发及调用
    git批量备份
    UDP端口扫描
    将markdown文件转换为pdf
    指定ssh key访问git
    CentOS6.2调整home分区大小
  • 原文地址:https://www.cnblogs.com/lknny/p/4727338.html
Copyright © 2020-2023  润新知