신호량이 다선정 통신에서 운용되다

25215 단어
동기화하는 세 가지 방법:
블록 동기화 또는 동기화 방법에서 사용해야 합니다.
  • notify () 는 현재 라인에 접근하지 않고 마운트된 라인으로 돌아갑니다
  • notifyALL()
  • wait() 마운트 해제 루틴 자원에 대한 접근 파라미터 시간이 있으면 특정한 시간을 기다렸다가 마운트 해제
  •  
    class CommonTalkVar{
          private char product;
          /**     
           *  true:       
           *  false:         
           * */
          private  boolean isProduced=false;
    
        /**
         * @description production method
         */
        public synchronized void putCommonData(char product){
                if(isProduced){
                    try {
                        System.out.println(Thread.currentThread().getName()+"          ");
                        wait();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                this.product=product;
                this.isProduced=true;
                System.out.println(Thread.currentThread().getName()+"     :"+this.product);
                notify();
        }
        /**
         * @description consumer method
         */
        public synchronized char getCommonData(){
            if(!isProduced){
                try {
                    System.out.println(Thread.currentThread().getName()+"          ");
                    wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            this.isProduced=false;
            System.out.println(Thread.currentThread().getName()+"     :"+this.product);
            notify();
            return this.product;
        }
    }
    
    class Consumer extends Thread{
        private CommonTalkVar commonTalkVar;
    
        public Consumer(CommonTalkVar commonTalkVar){
            this.commonTalkVar=commonTalkVar;
        }
    
        @Override
        public  void run() {
            char tempc;
            do{
                try {
                    Thread.sleep((int)(Math.random()*3000));
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                tempc=commonTalkVar.getCommonData();
            }while (tempc!='D');
        }
    }
    
    class Product extends Thread{
        private CommonTalkVar commonTalkVar;
        public Product(CommonTalkVar commonTalkVar){
            this.commonTalkVar=commonTalkVar;
        }
    
        @Override
        public  void run() {
            for (char i = 'A'; i <= 'D'; i++){
                try {
                    Thread.sleep((int)(Math.random()*3000));
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                commonTalkVar.putCommonData(i);
            }
        }
    }
    
    public class ConsumerAndProduct {
        public static void main(String[] args) {
            CommonTalkVar talkVar = new CommonTalkVar();
    
            new Consumer(talkVar).start();
            new Product(talkVar).start();
        }
    }

    다중 노드의 순서 제어
    실제로는 notifyAll () 방법의 제어 사용법을 주의하는 것이다
    package Thread;
    
    public class Demo1_Synchronized {
    
        /**
         * @param args
         *      
         */
        public static void main(String[] args) {
            final Printer p = new Printer();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print1();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print2();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print3();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    
    }
    
    class Printer {
        Demo d = new Demo();
    
        private static volatile int signal=1;
        
        public void print1() throws InterruptedException {
            synchronized (this) {
                while(this.signal!=1) {
                    this.wait();
                }
                this.signal=2;
                System.out.println("11111");
                this.notifyAll();
            }
        }
    
        public void print2() throws InterruptedException {
            synchronized (this) {
                while(this.signal!=2) {
                    this.wait();
                }
                this.signal=3;
                System.out.println("22222");
                this.notifyAll();
            }
        }
        
        public void print3() throws InterruptedException {
            synchronized (this) {
                while(this.signal!=3) {
                    this.wait();
                }
                this.signal=1;
                System.out.println("33333");
                this.notifyAll();
            }
        }
    }
    
    //     
    class Demo {
    }

    잠금 동기화
    package Thread;
    
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Test6 {
    
        public static void main(String[] args) {
            new Ticket12().start();
            new Ticket12().start();
            new Ticket12().start();
            new Ticket12().start();
        }
    
    }
    class Ticket12 extends Thread {
        private static volatile int ticket = 100;
        
        private static final ReentrantLock lock=new ReentrantLock();
        public void  run() {
            while (true) {
                    lock.lock();
                    try {
                        if (ticket <= 0) {
                            break;
                        }
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(getName() + "   " + ticket-- + "  ");
                        
                    } finally {
                        lock.unlock();
                    }
            }
        }
    }

    다시 잠글 수 있는 용법
    package Thread;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Demo1_ReentrantLock {
        public static void main(String[] args) {
            final PrinterReentrantLock p = new PrinterReentrantLock();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print1();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print2();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
            
            new Thread() {
                public void run() {
                    while(true) {
                        try {
                            p.print3();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    
    }
    
    class PrinterReentrantLock {
        
        private int signal=1;
        
        private static final ReentrantLock lock=new ReentrantLock();
        
        private static final Condition c1=lock.newCondition();
        private static final Condition c2=lock.newCondition();
        private static final Condition c3=lock.newCondition();
    
        public void print1() throws InterruptedException {
                lock.lock();
                try {
                    while (this.signal != 1) {
                        c1.await();
                    }
                    this.signal = 2;
                    System.out.println("11111");
                    c2.signal();
                } finally {
                    lock.unlock();
                }
        }
    
        public void print2() throws InterruptedException {
                lock.lock();
                try {
                    while (this.signal != 2) {
                        c2.await();
                    }
                    this.signal = 3;
                    System.out.println("22222");
                    c3.signal();
                } finally {
                    lock.unlock();
                }
        }
        
        public void print3() throws InterruptedException {
                lock.lock();
                try {
                    while (this.signal != 3) {
                        c3.await();
                    }
                    this.signal = 1;
                    System.out.println("33333");
                    c1.signal();
                } finally {
                    lock.unlock();
                }
        }
    }

    좋은 웹페이지 즐겨찾기