> Java > java지도 시간 > 본문

Java 스레드가 협력하는 방법은 무엇입니까?

王林
풀어 주다: 2023-05-17 20:34:15
앞으로
1435명이 탐색했습니다.

스레드는 왜 협력해야 하나요?

스레드는 특정 작업을 완료하기 위해 서로 협력합니다. 예를 들어 한 스레드가 개체의 값을 수정하고 다른 스레드가 변경 사항을 감지한 다음 해당 작업을 수행합니다. 스레드를 사용하고 최종 실행은 다른 스레드입니다. 이 모델을 통해 생산자와 소비자가 분리되어 '무엇'과 '어떻게'의 고립이 달성됩니다. 간단한 방법은 소비자 스레드가 계속 루프를 수행하여 변수가 기대치를 충족하는지 확인하고, while 루프에 만족되지 않은 조건을 설정하고, 조건이 충족되면 while 루프를 종료하여 소비자의 작업을 완료하는 것입니다. 이러한 스레드 간의 협업에는 두 가지 문제가 있습니다.

(1) 적시성을 보장하기가 어렵습니다.

(2) 간접비를 줄이는 것은 어렵습니다. 1밀리초 정도 자는 등 수면 시간을 줄이면 소비자는 상태 변화를 더 빨리 감지할 수 있지만 프로세서 자원을 더 많이 소모해 불필요한 낭비가 발생할 수 있다.

소개

Java에서 스레드 공동 작업의 가장 일반적인 두 가지 방법: Object.wait(), Object.notify() 사용 및 Condition

메서드 1 사용

Object의 wait, inform 및 informAll 메소드는 다음과 같이 정의됩니다. follow

  • public final 기본 void inform(); public final 기본 void wait(long timeout) throws InterruptedException(), inform() 및 informAll() 메서드는 다음과 같습니다. 로컬 메서드이며 최종 메서드이므로 재정의할 수 없습니다

  • 객체의 wait() 메서드를 호출하여 현재 스레드를 차단하고 현재 스레드는 이 객체의 모니터(예: 잠금)를 소유해야 합니다

  • 객체 호출 inform() 메소드는 이 객체의 모니터를 기다리고 있는 스레드를 깨울 수 있습니다. 이 객체의 모니터를 기다리는 스레드가 여러 개인 경우 스레드 중 하나만 깨울 수 있습니다.

  • informAll() 메서드를 호출하면 이 개체의 모니터를 기다리고 있는 모든 스레드를 깨울 수 있습니다.

  • 이 세 가지 메서드를 Object 클래스에 선언하는 이유는 각각 개체에 모니터(예: 잠금)가 있습니다

  • 개체의 wait() 메서드, 현재 스레드를 호출합니다. 이 개체의 모니터(예: 잠금)를 소유해야 하므로 wait() 메서드 호출은 동기화된 블록 또는 동기화된 메소드

  • public class Test {
        public static Object object = new Object();
        public static void main(String[] args) {
            Thread1 thread1 = new Thread1();
            Thread2 thread2 = new Thread2();
            thread1.start();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            thread2.start();
        }
        static class Thread1 extends Thread{
            @Override
            public void run() {
                synchronized (object) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                    }
                    System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁");
                }
            }
        }
        static class Thread2 extends Thread{
            @Override
            public void run() {
                synchronized (object) {
                    object.notify();
                    System.out.println("线程"+Thread.currentThread().getName()+"调用了object.notify()");
                }
                System.out.println("线程"+Thread.currentThread().getName()+"释放了锁");
            }
        }
    }
    로그인 후 복사
  • 실행 결과

Thread-1이 object.notify()를 호출함

Thread Thread-1이 잠금을 해제함

Thread Thread-0이 잠금을 획득함


메서드 2

조건은 Java 1.5에서만 나타났습니다. 기존 Object의 wait() 및 inform()을 대체하는 데 사용됩니다. 스레드 간 협업을 위해서는 Object의 wait() 및 inform()을 사용하는 것보다 사용하는 것이 더 안전하고 효율적입니다. 스레드 간 협업을 위한 Condition1의 wait() 및 signal()

  • Condition은 인터페이스이며 기본적으로 메서드는 wait() 및 signal() 메서드입니다

  • Condition은 Lock 인터페이스에 따라 생성됩니다. 조건은 lock.newCondition()

  • 조건의 wait() 및 signal() 메서드를 호출합니다. 둘 다 필요합니다. 잠금 보호 내에서는 lock.lock()과 사이에 사용해야 합니다. lock.unlock

  • public class Test {
        private int queueSize = 10;
        private PriorityQueue<Integer> queue = new PriorityQueue<Integer>(queueSize);
        private Lock lock = new ReentrantLock();
        private Condition notFull = lock.newCondition();
        private Condition notEmpty = lock.newCondition();
        public static void main(String[] args)  {
            Test test = new Test();
            Producer producer = test.new Producer();
            Consumer consumer = test.new Consumer();
            producer.start();
            consumer.start();
        }
        class Consumer extends Thread{
            @Override
            public void run() {
                consume();
            }
            private void consume() {
                while(true){
                    lock.lock();
                    try {
                        while(queue.size() == 0){
                            try {
                                System.out.println("队列空,等待数据");
                                notEmpty.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.poll();                //每次移走队首元素
                        notFull.signal();
                        System.out.println("从队列取走一个元素,队列剩余"+queue.size()+"个元素");
                    } finally{
                        lock.unlock();
                    }
                }
            }
        }
        class Producer extends Thread{
            @Override
            public void run() {
                produce();
            }
            private void produce() {
                while(true){
                    lock.lock();
                    try {
                        while(queue.size() == queueSize){
                            try {
                                System.out.println("队列满,等待有空余空间");
                                notFull.await();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.offer(1);        //每次插入一个元素
                        notEmpty.signal();
                        System.out.println("向队列取中插入一个元素,队列剩余空间:"+(queueSize-queue.size()));
                    } finally{
                        lock.unlock();
                    }
                }
            }
        }
    }
    로그인 후 복사

위 내용은 Java 스레드가 협력하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!