상세 컨텐츠

본문 제목

[쓰레드 넘버프린트] wait notify 교대로 가다가 안되는거 해결

자바

by esoesmio 2023. 4. 5. 14:04

본문

해결법

 

 

 


//넘버프린터 선생은 어케했는지 보기

public class z3 {

    public static void main(String[] args) {


        NumberPrinter np = new NumberPrinter();

        Thread1 t1 = new Thread1();
        Thread2 t2 = new Thread2();

        t1.setNp(np);
        t2.setNp(np);

        t1.start();
        t2.start();


    }
}

class NumberPrinter {
    private int num;
    private static boolean isThreadFinished = false;

    public synchronized void numPrint1(int a) {
        this.num = a;

        for(int i = this.num; i <= 50; i++) {
            if(i % 3 == 0) {
                System.out.println("print1 : " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.out.println("print1 실행");
            }

            if(i == 50) {
                isThreadFinished = true;
            }

            notify();

            if(!isThreadFinished) {

                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
// 3번문제
// public void numPrint1(int a) {
//    this.num = a;
//
//    for(int i = this.num; i <= 50; i++) {
//       if(i % 3 == 0) {
//          System.out.println("print1 : " + i);
//
//          try {
//             Thread.sleep(300);
//          } catch (InterruptedException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//          }
//       }
//    }
// }

// 4번문제
// public synchronized void numPrint1(int a) {
//    this.num = a;
//
//    for(int i = this.num; i <= 50; i++) {
//       if(i % 3 == 0) {
//          System.out.println("print1 : " + i);
//
//          try {
//             Thread.sleep(300);
//          } catch (InterruptedException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//          }
//       }
//    }
// }

    public synchronized void numPrint2(int a) {
        this.num = a;

        for(int i = this.num; i <= 100; i++) {
            if(i % 5 == 0 && i % 7 == 0) {
                System.out.println("print2 : " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                System.out.println("print2 실행");
            }

            if(i == 100) {
                isThreadFinished = true;
            }

            notify();
            if(!isThreadFinished) {

                try {
                    wait();
                } catch (InterruptedException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
// 3번 문제
// public void numPrint2(int a) {
//    this.num = a;
//    for(int i = this.num; i <= 100; i++) {
//       if(i % 5 == 0 && i % 7 == 0) {
//          System.out.println("print2 : " + i);
//
//          try {
//             Thread.sleep(300);
//          } catch (InterruptedException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//          }
//       }
//    }
// }

// 4번 문제
// public void numPrint2(int a) {
//    synchronized(this) {
//       this.num = a;
//    }
//    for(int i = this.num; i <= 100; i++) {
//       if(i % 5 == 0 && i % 7 == 0) {
//          System.out.println("print2 : " + i);
//
//          try {
//             Thread.sleep(300);
//          } catch (InterruptedException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//          }
//       }
//    }
// }
}
class Thread1 extends Thread{
    private NumberPrinter np;

    public NumberPrinter getNp() {
        return np;
    }

    public void setNp(NumberPrinter np) {
        this.np = np;
    }

    @Override
    public void run() {
        np.numPrint1(20);
    }
}

class Thread2 extends Thread{
    private NumberPrinter np;

    public NumberPrinter getNp() {
        return np;
    }

    public void setNp(NumberPrinter np) {
        this.np = np;
    }

    @Override
    public void run() {
        np.numPrint2(30);
    }
}

'자바' 카테고리의 다른 글

[Function] 사용방식  (0) 2023.04.05
[이넘] 윤달구하기 일수의 총합  (0) 2023.04.05
[람다 생성자연습] 영어학원 컴학원  (0) 2023.04.05
[이넘]2  (0) 2023.04.05
[이넘]1  (0) 2023.04.05

관련글 더보기

댓글 영역