-4
class A 
{ 
    public void func() 
    { 
     new Thread() 
     { 
      public void run() 
      { 
       // statements 
      } 
     } .start(); 
     new Thread() 
     { 
      public void run() 
      { 
       // statements 
      } 
     } .start(); 
     new Thread() 
     { 
      public void run() 
      { 
       // statements 
      } 
     } .start(); 
     new Thread() 
     { 
      public void run() 
      { 
       // statements 
      } 
     } .start(); 
    } 
} 

여기서는 처음 두 스레드 (쌍 A)가 동시에 실행되고 다음 두 스레드 (쌍 B)가 쌍 A가 실행을 완료 한 후에 만 ​​동시에 실행하려고합니다. 이 java.util.concurrent의 또는 쓰레드 그룹 통해 달성 될 수 있다면 또한 만약 누군가 설명 할 수있다. 나는 정말로 도움이 될만한 도움을 주실 것입니다.pairwise 스레드를 동시에 실행

+0

는 아무도 나에게 다운 투표의 이유를 설명 할 수 있습니까? – Vanessa

+0

새로운 사용자가 여기에 질문을하면 질문이 내려집니다. 와우, 좋은 정신력이야. – Vanessa

답변

0
public void func() 
{ 
    Thread a = new Thread() 
    { 
     public void run() 
     { 
      // statements 
     } 
    } 
    Thread b = new Thread() 
    { 
     public void run() 
     { 
      // statements 
     } 
    } 
    a.start(); 
    b.start(); 
    a.join(); //Wait for the threads to end(); 
    b.join(); 
    new Thread() 
    { 
     public void run() 
     { 
      // statements 
     } 
    } .start(); 
    new Thread() 
    { 
     public void run() 
     { 
      // statements 
     } 
    } .start(); 
} 
+0

하지만 a와 b가 동시에 실행될 것이라고 보장합니까? 또한 두 개의 스레드가 더 있습니까? – Vanessa

+0

@Vanessa 예, 예. – awksp

+0

예 모두 감사합니다! :) – Vanessa

0

CountDownLatch을 사용하면 특정 스레드 수가 countDown()이 될 때까지 대기 할 수 있습니다.이 시점에서 주 스레드는 계속 진행할 수 있습니다. 즉, 당신이 그들에게 래치를 통과해야합니다, 당신은 래치의 카운트가 결국 0에 도달 할 수 있도록 그들이 자신의 작업으로 수행 할 때 그들이 latch.countDown() 전화 있어야합니다 - 당신은 당신의 스레드에 일부 변경을해야 할 것이다.

그래서 메인 클래스는 같은 보일 것이다 :

final CountDownLatch latch = new CountDownLatch(2); // Making this final is important! 
// start thread 1 
// start thread 2 
latch.await(); // Program will block here until countDown() is called twice 
// start thread 3 
// start thread 4 

을 그리고 당신의 스레드가 같은 것을 보일 것이다 :

new Thread() { 
    public void run() { 
     // do work 
     latch.countDown() 
    } 
} 

을 그리고 처음 두 스레드가 래치는 주 스레드 수를 완료 한 번만 계속해서 다른 두 세를 시작하십시오. eads.

+0

스레드가 countDown을 어떻게 호출 할 지 조금 설명해 주시겠습니까? – Vanessa

+0

' .countDown()' 내 대답에 조금 확장됩니다 – awksp

+0

친절하게 상관 없어요. :) – Vanessa