使用线程1打印编号1,2,3,使用线程2打印编号4,5,6,使用线程3打印编号7,8,9,再使用线程1打印编号10,11,12

时间:2019-05-26 17:53:30

标签: java multithreading wait notify

我正在尝试编写一个带有等待和通知的简单程序,在其中我将创建3个线程。

  

第一个线程应打印1、2、3。

     

第二个线程应打印4、5、6。

     

第三个线程应打印7、8、9。

     

然后,第一个线程应打印10、11、12等。

以下是同一练习的示例代码,但我无法打印所需的输出。

LinearLayout listlayout = (LinearLayout) findViewById(R.id.layout_list);
listlayout.setClickable(true);

listlayout.setOnClickListener(new View.OnClickListener()
 {
  @Override
  public void onClick(View v)
    {
    /// do your stuff here 
    }
  });

输出如下:

public class MyThread2 extends Thread {

    public final static Object obj = new Object();

    int threadNo;   
    static volatile int threadNoToRun;
    static volatile int counter = 1;

    public MyThread2(int threadNo){
        this.threadNo= threadNo;
    }

    @Override
    public void run() {
        synchronized (obj) {
                try {
                    if(threadNoToRun != threadNo)
                        obj.wait();
                    else{
                        for(int i = 1 ; i < 4 ; i++){
                            if(threadNoToRun == threadNo){
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                System.out.println(threadNo + " counter value is "+counter);
                                counter++;
                                if(threadNoToRun == 1){
                                    threadNoToRun = 2;
                                }
                                else if(threadNoToRun == 2){
                                    threadNoToRun = 3;
                                }
                                else if(threadNoToRun == 3){
                                    threadNoToRun = 1;
                                }
                            }
                        }

                        obj.notifyAll();                            
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

        }    
    }

    public static void main (String args[]) {

        /*
         * Creating as many threads as needed.
         */
        MyThread2 th1 = new MyThread2(1);
        MyThread2 th2 = new MyThread2(2);
        MyThread2 th3 = new MyThread2(3);
        MyThread2.threadNoToRun = 1;
        th1.start();
        th2.start();
        th3.start();
    }
}

1 个答案:

答案 0 :(得分:2)

这里,只是一些更改。

尽管如此,我必须指出,这种并发并不会提高计算速度。始终只有一个线程处于活动状态。

public class MyThread2 extends Thread {

    public final static Object obj = new Object();

    int threadNo;
    static volatile int threadNoToRun;
    static volatile int counter = 1;

    public MyThread2(int threadNo) {
        this.threadNo = threadNo;
    }

    @Override
    public void run() {
        synchronized (obj) {
            try {
                while (counter < 100) {
                    if (threadNoToRun != threadNo)
                        obj.wait();
                    else {
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        System.out.println(threadNo + " counter value is " + counter);
                        counter++;
                        if (threadNoToRun == 1) {
                            threadNoToRun = 2;
                        } else if (threadNoToRun == 2) {
                            threadNoToRun = 3;
                        } else if (threadNoToRun == 3) {
                            threadNoToRun = 1;
                        }

                        obj.notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {

        /*
         * Creating as many threads as needed.
         */
        MyThread2 th1 = new MyThread2(1);
        MyThread2 th2 = new MyThread2(2);
        MyThread2 th3 = new MyThread2(3);
        MyThread2.threadNoToRun = 1;
        th1.start();
        th2.start();
        th3.start();
    }
}
相关问题