스레드를 더 잘 이해하려고 노력하므로 간단한 작업을 구현하고 있습니다.스레드 깨우기 및 깨우기 스레드
나는 runnable을 구현하는 2 개의 클래스가 있습니다. 각각은 1에서 10까지의 2 개의 임의의 정수를 생성합니다. ClassA는 합계를 계산하고 ClassB는 곱셈을 계산합니다. 두 사람 모두 15 초 동안이 작업을 반복하고 있습니다.
두 개의 정적 및 동기화 된 메서드 setVal 및 getVal이있는 General이라는 다른 클래스가 있습니다. 각 스레드는 각 계산/반복 후에 General.setVal (result)를 호출합니다. setVal은 이전 값보다 숫자에 더 가깝기 만 값을 설정합니다. getValue는 값만 가져옵니다.
각 스레드를 시작하는 메인 클래스가 있습니다. 그런 다음 스레드에 의해 설정된 값을 출력하는 20 초 동안 루프가 있습니다. 그래서 getValue를 호출하고 그것을 인쇄합니다.
하나의 반복 작업 후에 각 스레드가 기다리고 다른 하나에게 반복 작업을 알려주는 등 ... 어떻게 처리 할 수 있습니까?
public class Particle1 implements Runnable{
//private int x;
private static final int max = 10;
private static final int min = 1;
public void run(){
long t= System.currentTimeMillis();
long end = t+15000;
while(System.currentTimeMillis() < end) {
Random rand = new Random();
int a = rand.nextInt((max - min) + 1) + min;
int b = rand.nextInt((max - min) + 1) + min;
int x = a+b;
System.out.println("P1: "+a+"+"+b+"="+x);
Gather.setRes(x);
//i want it here to sleep until the other one wakes it up.
}
}
}
public class Particle2 implements Runnable{
//private int x;
private static final int max = 10;
private static final int min = 1;
public void run(){
long t= System.currentTimeMillis();
long end = t+15000;
while(System.currentTimeMillis() < end) {
Random rand = new Random();
int a = rand.nextInt((max - min) + 1) + min;
int b = rand.nextInt((max - min) + 1) + min;
int x = a+b;
System.out.println("P2: "+a+"+"+b+"="+x);
Gather.setRes(x);
//i want it here to sleep until the other one wakes it up.
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new Particle1());
Thread thread2 = new Thread(new Particle2());
thread1.start();
thread2.start();
long t= System.currentTimeMillis();
long end = t+20000;
while(System.currentTimeMillis() < end) {
System.out.println("Minimum is: "+Gather.getRes());
Thread.sleep(1000);
}
return;
}
}
public class Gather {
public Gather() {
// TODO Auto-generated constructor stub
}
private static int res=1000000;
public static int getRes() {
return res;
}
public synchronized static void setRes(int inres) {
if(Math.abs(inres-250)<res){
res = inres;
}
}
}
두 스레드가 적대적으로 작동하는 것을 의미합니까? (일종의) 멀티 스레딩의 목적을 이기기 때문에! – jbutler483
많은 초보자가 두 개 이상의 스레드를 번갈아 가며 만드는 방법을 묻습니다. 그렇게 할 동기 객체를 만들 수 있지만 그와 같은 객체는 찾을 수 없습니다 왜냐하면 프로그램은 두 개 이상의 스레드가 동시에 작동 할 수 없기 때문에 하나의 스레드가 모든 스레드를 수행하도록함으로써 프로그램을 더 간단하게 만들 수 있습니다. 스레드의 전체 요점은 두 개 이상의 스레드가 동시에 다른 일을 할 수 있다는 것입니다. –