2017-05-23 8 views
-1

나는 두 개의 스레드 t1t2을 가지고 있으며 각각은 개별 작업을 수행합니다.자바 스레드 완료 상태

스레드 t1에서 작업이 60 % 완료된 후 스레드 t2를 시작하고 싶습니다.

아무도 아이디어를 얻을 수 있습니까?

+1

60 % 완성을 어떻게 알 수 있습니까? releavent 코드를 제공하지 않아 도움이되지 않습니다. – glee8e

+0

그게 내 질문은 .. 거기에 어떤 내장 자바 라이브러리에서 라이브러리를 찾을 수 있습니까? –

+0

당신이 함수를 요구하는 것과 같은 소리 _h (A) _ 여기서 _A_은 임의의 다른 함수 인 _a (...) _에 대한 _activation record_입니다. _a (...) _ 호출이 반환되기 전에 _h (A) _가 경과 할 시간을 계산하기를 원할 것입니다. 정품 인증 레코드가 Java에서 _reflect_ 할 수있는 개체가 아니라는 점을 기억하십시오. 더 큰 장애물은 _h (A) _를 사용하여 [중단 문제] (https://en.wikipedia.org/wiki/Halting_problem)를 해결할 수 있다는 것입니다. –

답변

0
@Test 
public void test() throws InterruptedException{ 
    Thread t = new Thread(new Task1()); 

    t.start(); 

    t.join(0); 

    //keep in mind that t2 can still be running at this point 
    System.out.println("done running task1."); 
} 

public static class Task1 implements Runnable{ 

    public void run(){ 
     //Smimulate some long running job. In your case you need to have your own logic of checking if t1 is 60% done. 
     //In this case we just wait 0.5 seconds for each 10% of work done 
     for (int i = 0; i < 10; i++){ 

      try { Thread.sleep(500); } 
      catch (InterruptedException e) { throw new RuntimeException(e); } 

      int percentComplete = i*10; 

      System.out.println("Completed " + percentComplete + "%."); 

      if (percentComplete == 60){ 
       new Thread(new Task2()).start(); //this how to start t2 when we are 60% complete 
      } 
     }    
    } 
} 

public static class Task2 implements Runnable{ 

    @Override 
    public void run() { 
     //simulate t2 task that will run for 5 seconds. 

     System.out.println("task2 started."); 

     try { Thread.sleep(5000); } 
     catch (InterruptedException e) { throw new RuntimeException(e); } 

     System.out.println("task2 is done."); 
    } 

} 
0

T1은 60 % 완료 시점을 알 수 있습니까? 그렇다면 왜 그 시간에 T2를 시작하지 않습니까?

Thread t2 = null; 

class T2task implements Runnable { 
    ... 
} 

class T1task implements Runnable { 
    @Override 
    public void run() { 
     while (isNotFinished(...)) { 
      if (isAtLeast60PercentDone(...) && t2 != null) { 
       t2 = new Thread(new T2task(...)); 
       t2.start(); 
      } 
      doSomeMoreWork(...); 
     } 
    } 
}