2016-08-10 3 views
0

스레드가 시작되지 않습니다. 다른 스레드가 너무 많을 때다른 스레드가 너무 많을 때 스레드가 시작되지 않습니다.

멀티 스레드 응용 프로그램이 있습니다. 두 스레드의 워크 플로를 분석하고 있습니다. Thread_1은주기가 for(...)입니다. Thread_2에는 약간의 직업이 있습니다. 어떤 경우에는 Thread_2for(...) 사이클이 Thread_1으로 끝나지 않은 동안 작업을 시작하지 않는 경우가 있습니다. 시스템이 Thread_1에 대한 모든 리소스를 저장하기로 결정할 수 있습니까? Thread_2을 시작할 가능성을 부여하는 동안 Thread_1for(...)에 있습니다. 거기에 Thread.sleep(100) 같은 것을 넣어야합니까? 모든 것은 Java 1.4입니다.

+1

귀하의 응용 프로그램이 실행하지 않는 실행 순서 제약에 의존해서는 안된다. 그거야? –

+0

'Thread # yield()'를 사용해 볼 수도 있지만 일반적으로 필요하지 않을 때는 사용하지 않는 것이 좋습니다. 두 스레드를 병렬로 실행하는 것이 정말로 중요합니까? 그리고 그렇다면 그들이 무엇을하고 있으며 그들 사이에 어떤 동기화/메시징이 일어나고 있습니까? 스레드 1은 우선 순위가 낮은 백그라운드 스레드 일 수 있지만 스레드 2는 우선 순위가 높아야하므로 'Thead # setPriority()'를 사용해보십시오. – Thomas

+0

그건 쓰레드가있는 것입니다; 당신은 거기에서 많은 통제력을 가지고 있지 않습니다. 그런 의미에서 : 이미 ** 측정 ** 시작한 것 같습니다. 그러니 한발 더 나아가서 수면을 추가하는 것이 어떻게 바뀌는지를 실험 해보십시오. 그런 다음 java 1.4; 진지하게? 오라클이 Java 버전의 "end of life"날짜를 추적하는 테이블에이 버전이 더 이상 나열되지 않는다는 것을 알고 있습니다. – GhostCat

답변

1

일부 코드 조각을 공유하면 멋지 겠지만 논리를 찾지 않고 코드를 디버그하는 것은 어렵습니다. 이상적으로 thread_1과 thread_2는 독립적으로 실행되어야합니다. thread_2는 thread_1의 루프 완료를 기다릴 수 없습니다. 예 :

class RunnableDemo implements Runnable { 
    private Thread t; 
    private String threadName; 

    RunnableDemo(String name){ 
     threadName = name; 
     System.out.println("Creating " + threadName); 
    } 
    public void run() { 
     System.out.println("Running " + threadName); 
     try { 
     for(int i = 4; i > 0; i--) { 
      System.out.println("Thread: " + threadName + ", " + i); 
      // Let the thread sleep for a while. 
      Thread.sleep(50); 
     } 
    } catch (InterruptedException e) { 
     System.out.println("Thread " + threadName + " interrupted."); 
    } 
    System.out.println("Thread " + threadName + " exiting."); 
    } 

    public void start() 
    { 
     System.out.println("Starting " + threadName); 
     if (t == null) 
     { 
     t = new Thread (this, threadName); 
     t.start(); 
     } 
    } 

} 

public class TestThread { 
    public static void main(String args[]) { 

     RunnableDemo R1 = new RunnableDemo("Thread-1"); 
     R1.start(); 

     RunnableDemo R2 = new RunnableDemo("Thread-2"); 
     R2.start(); 
    } 
} 

출력 :

Creating Thread-1 
Starting Thread-1 
Creating Thread-2 
Starting Thread-2 
Running Thread-1 
Thread: Thread-1, 4 
Running Thread-2 
Thread: Thread-2, 4 
Thread: Thread-1, 3 
Thread: Thread-2, 3 
Thread: Thread-1, 2 
Thread: Thread-2, 2 
Thread: Thread-1, 1 
Thread: Thread-2, 1 
Thread Thread-1 exiting. 
Thread Thread-2 exiting. 
-1

당신이 반복의 특정 숫자 후를위한 루프 일시 정지에 대한 첫 번째 스레드을하고 한 번 Thread_1 시간을 허리를하지 false로 정적 var에 th2_done을 설정할 수 있습니다 Thread_2는

Thread_1 수행됩니다에 대한 (...) { 을 (num_it %의주기 &경우 10 th2_done == false)를 절전 (100);}

Thread_2 : (...) {} th2_done에 대한 = 사실

관련 문제