Java/Excercise

thread ch03 JoinTest

낭구리 2021. 9. 8. 18:18
package ch03;

public class JoinTest extends Thread {

	int start;
	int end;
	int total;

	public JoinTest(int start, int end) {
		this.start = start;
		this.end = end;

	}

	@Override
	public void run() {
		int i;
		for (i = start; i <= end; i++) {
			total += i;
		}
	}// run을 쓰기위해 위에 변수 선언

	public static void main(String[] args) {

		JoinTest jt1 = new JoinTest(1, 50);//1~50
		JoinTest jt2 = new JoinTest(51, 100); //51~100 더한값
        jt1.start();
        jt2.start();
        
        //주최는 mainThread
        try {
			jt1.join();
			jt2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} //jt1 끝나면 jt2 끝나면 lastTotal값을 출력
        //잘쓰이진않음
        
        int lastTotal = jt1.total + jt2.total;
        System.out.println("jt1.total = " +jt1.total);
        System.out.println("jt2.total = " +jt2.total);
        System.out.println("lastTotal = " + lastTotal);
	}
	

}

'Java > Excercise' 카테고리의 다른 글

swing ch06 MiniAmongUs  (0) 2021.09.08
thread ch03 TerminateThread  (0) 2021.09.08
thread ch03 priorityTest  (0) 2021.09.08
thread ch02 SharedResource  (0) 2021.09.08
thread ch01 RunnableTest2  (0) 2021.09.08