我想通过线程1打印1,
               2通过线程2,
               3由Thread-3
像123123123等

我想使用仅一个由多个线程共享的可运行对象来实现
我尝试过的方式是
已使用一个atomicInteger作为信号来指示应打印的内容
并使用了wait和notify来在线程之间转移控制权
使用threadName检查以允许特定线程打印

我已经尝试过像下面这样,但它无法正常工作,有人可以在此帮助我。

/** Main class which creates 3 threads using one runnable and
    starts the 3 threads */
package PrintOneTwoThree;
public class MainClass
{
  public static void main(String args[])
  {
    //one runnable is created and shared by 3 threads
    final PrintNumbersConsecutively printRunnable = new
                               PrintNumbersConsecutively();
    Thread t1 = new Thread(printRunnable, "Thread1");
    Thread t2 = new Thread(printRunnable,"Thread2");
    Thread t3 = new Thread(printRunnable,"Thread3");

    t1.start();
    t2.start();
    t3.start();
 }
}

/** Each of the three threads are made to print 1 ,2 and 3 */
package PrintOneTwoThree;
import java.util.concurrent.atomic.AtomicInteger;
public class PrintNumbersConsecutively implements Runnable
{
  private Object monitor = new Object();
  AtomicInteger atomicInt = new AtomicInteger(1);

@Override
public void run()
{
    System.out.println(Thread.currentThread().getName()+"Started");
    // TODO Auto-generated method stub
    printNumbers();
}

// method to print the numbers using 3 different threads 1 ,2 and 3
public void printNumbers()
{
    try
    {
        while(true)
        {
            synchronized (monitor)
            {
                System.out.println(Thread.currentThread().getName()+"held
                               the lock of monitor");
                System.out.println("printVal is"+ atomicInt);

                 //Thread1 should print 1
                if(atomicInt.get() == 1 &&
                        Thread.currentThread().getName().equals("Thread1"))
                {
                    //prints 1 and next awakes second thread by setting
                    // atomic int to 2
                    System.out.println(1);
                    atomicInt.set(2);
                    monitor.notifyAll();

              System.out.println(Thread.currentThread().getName()+"Releasing
              the lock of monitor");
                    monitor.wait();
                }

                //Thread 2 should print 2
                if(atomicInt.get() == 2 &&
                     Thread.currentThread().getName().equals("Thread2"))
                {
                    //prints 2 and next awakes third thread by setting
                    // atomic int to 3
                    System.out.println(2);
                    atomicInt.set(3);
                    monitor.notifyAll();
                    monitor.wait();
                }

                //Thread 3 should print 3
                if(atomicInt.get() == 3 &&
                   Thread.currentThread().getName().equals("Thread3"))
                {
                    //prints 3 and next awakes first thread by setting
                    // atomic int to 1
                    System.out.println(3);
                    atomicInt.set(1);
                    monitor.notifyAll();
                    monitor.wait();
                }
            }
        }

    }
    catch(InterruptedException e)
    {
        //catches the interrupted excpeiton
    }
}
}


输出是

printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁
printVal is1
Thread2持有监视器的锁

最佳答案

我对代码进行了以下更改


添加等待
更改条件

public class Practice {
public static void main(String args[])
{
    //one runnable is created and shared by 3 threads
    final PrintNumbersConsecutively printRunnable = new
    PrintNumbersConsecutively();
    Thread t1 = new Thread(printRunnable, "Thread1");
    Thread t2 = new Thread(printRunnable,"Thread2");
    Thread t3 = new Thread(printRunnable,"Thread3");

    t1.start();
    t2.start();
    t3.start();
 }
}

 /** Each of the three threads are made to print 1 ,2 and 3 */

class PrintNumbersConsecutively implements Runnable
{
private Object monitor = new Object();
AtomicInteger atomicInt = new AtomicInteger(1);

static boolean one = true;
static boolean two = false;
static boolean three = false;

@Override
public void run()
{
    System.out.println(Thread.currentThread().getName()+"Started");
    printNumbers();
}

// method to print the numbers using 3 different threads 1 ,2 and 3
public void printNumbers()
{
    try
    {
        int i =0;
        while(i < 30)
        {
            synchronized (monitor)
            {
                /*System.out.println(Thread.currentThread().getName()+" held the lock of monitor");
                System.out.println("printVal is"+ atomicInt);*/
                //Thread1 should print 1
                if(Thread.currentThread().getName().equals("Thread1")) {
                    if (atomicInt.get() == 1) {
                        //prints 1 and next awakes second thread by setting
                        // atomic int to 2
                        System.out.print(1 + " ");
                        atomicInt.set(2);
                        monitor.notifyAll();
                       // System.out.println(Thread.currentThread().getName() + "Releasing the lock of monitor");
                    } else {
                        monitor.wait();
                    }
                }

                if(Thread.currentThread().getName().equals("Thread2")) {
                    if (atomicInt.get() == 2) {
                        //prints 1 and next awakes second thread by setting
                        // atomic int to 2
                        System.out.print(2 + " ");
                        atomicInt.set(3);
                        monitor.notifyAll();
                      //  System.out.println(Thread.currentThread().getName() + "Releasing the lock of monitor");
                    } else {
                        monitor.wait();
                    }
                }

                if(Thread.currentThread().getName().equals("Thread3")) {
                    if (atomicInt.get() == 3) {
                        //prints 1 and next awakes second thread by setting
                        // atomic int to 2
                        System.out.print(3 + " ");
                        atomicInt.set(1);
                        monitor.notifyAll();
                        //System.out.println(Thread.currentThread().getName() + "Releasing the lock of monitor");
                    } else {
                        monitor.wait();
                    }
                }
            }
            i++;
        }

    }
    catch(InterruptedException e)
    {
        //catches the interrupted excpeiton
    }
}


}


输出:

Thread3Started
Thread1Started
Thread2Started
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2

08-04 21:26