자바에서 MT와 관련된 간단한 문제가 있습니다. 콘솔에 대한 액세스를 동기화하려면 woudlike. 예 : System.out에서 첫 번째 스레드 쓰기 "Number one"Thread2 printl ("Number two"). 나는 소리내어이 스레드를 버퍼링하지 않고 콘솔에서 순차적으로 numer를 쓰는 것과 동기화하고 싶다. 어떻게해야합니까?콘솔과 공유 된 스레드
Thread one
Thread two
Thread one
Thread two
...
// 코드
package com.example;
public class MyThread implements Runnable{
@Override
synchronized public void run(){
while(true){
System.out.println("Thread first");
}
}
}
//
package com.example;
public class MyThread2 implements Runnable {
@Override
synchronized public void run() {
// TODO Auto-generated method stub
System.out.println("");
}
}
//
package com.example;
import java.util.concurrent.Semaphore;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread th1= new Thread(new MyThread());
Thread th2= new Thread(new MyThread2());
th2.setPriority(Thread.MAX_PRIORITY);
th1.start();
th2.start();
}
}
기존 코드에 문제가있는 경우 그 중 일부를 게시하십시오. 지금까지 뭐 해봤 어? 또한, 왜 [태그 : 숙제] 또는 일종의 학습 연습 이외에이 작업을하고 싶은지 잘 모르겠습니다. 숙제 인 경우 해당 태그를 질문에 추가하십시오. 따라서 힌트를 제공 할 수 있습니다. –
순차적으로 쓰고 싶다면 쓰레드를 사용하는 이유는 무엇입니까? 이것은 단순히 관심있는 질문입니까? –
대체하려면 두 개의 스레드가 필요합니까? 아니면 어떤 수의 스레드가 필요합니까?이 예제는 두 가지일까요? – corsiKa