Bài tập về Thread trong Java

Em mới đọc về phần Thread trong Java mong ae giúp đỡ @@
E có đề bài như sau:

và đây là code của em khi chạy nó bị treo: https://ideone.com/XpJ6L4

*Edit: e đã thử thêm 1 biến boolean về việc đã nhập 2 số chưa thì kết quả sẽ luôn đều đặn 1 nhập và 1 in ra đều và khác với kết quả trong đề bài mặc dù đề bài ghi output có thể như vậy:
https://ideone.com/90ozdr

I’ll help you post the code:

import java.util.Scanner;

public class Main {
    static class SharedData{
        public int a,b;
//
//
//        public int getA() {
//            return a;
//        }
//
//        public void setA(int a) {
//            this.a = a;
//        }
//
//        public int getB() {
//            return b;
//        }
//
//        public void setB(int b) {
//            this.b = b;
//        }
    }
    static class ThreadOne extends Thread{
        final SharedData sharedData;
        public ThreadOne(SharedData sharedData){
            this.sharedData = sharedData;
        }

        @Override
        public void run() {
            Scanner scanner = new Scanner(System.in);
            while (true){
                synchronized (sharedData){
                    System.out.print("Enter the first number: ");
                    int t = scanner.nextInt();
                    sharedData.a=t;
                    System.out.print("Enter the second number: ");
                    t = scanner.nextInt();
                    sharedData.b=t;
                    try {
                        sharedData.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sharedData.notify();
                }
            }
        }
    }

    static class ThreadTwo extends Thread{
        final SharedData sharedData;

        public ThreadTwo(SharedData sharedData){
            this.sharedData = sharedData;
        }

        @Override
        public void run() {
            while(true){
                synchronized (sharedData){
                    try {
                        sharedData.wait();

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Addition Result: "+(sharedData.a+sharedData.b));
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sharedData.notify();
                }
            }
        }
    }

    public static void main(String[] args) {
        SharedData sharedData = new SharedData();
        ThreadOne t1 = new ThreadOne(sharedData);
        ThreadTwo t2 = new ThreadTwo(sharedData);
        t1.start();
        t2.start();
    }
}

Bất cứ action nào mà có interact with user thì phải chạy trên main thread, vd: user input, user vẽ, etc. Sau khi lấy được input rồi mới tạo 1 thread chạy background, suggest nhiêu đây là bạn đủ hiểu rồi.

5 Likes

Theo hình, mình nghĩ có dùng đến mảng (danh sách) để lưu các tham số người dùng nhập vào, t2 sẽ dùng từ mảng đấy.
FIFO, nên dùng Queue.

4 Likes

nhưng mà đề bài là nhập xong thì cái kia bắt đầu in ra kết quả Addition thì e nghĩ không cần dung tới queue @SITUVN.gcd

cái này là mình thực hiện input ở psvm xong rồi mới gọi đến 2 Thread kia hả a?

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?