我想要输出的说明:
我希望两个线程Gaurav和john完成一个while循环(从1到8),这样无论哪个线程开始ist,运行5次迭代(即直到count = 5),然后进入睡眠状态,然后下一个线程完成循环(运行count = 6到count = 8)。程序应该到那时结束。
对于这种情况,我创建了一个实例变量count并以计数器方法递增其值

问题是:即使在同步计数器方法(计数器方法递增计数变量)后,我仍得到一个奇怪的输出(输出附加在末尾)

public class ThreadCounter implements Runnable {
   private int count;
   @Override
   public void run(){

      try {
            counter(); // call synchronized counter function

       } catch (InterruptedException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
         }

     }//end of run()

    public synchronized void counter() throws InterruptedException{

           while(true){

           // i want the loop to increment count from 1 to 4 by ist      thread(whichever thread runs ist) and then 5 to 8 by next thread

                 System.out.println("Which thread running ? " +Thread.currentThread().getName());

                 count++;

                 System.out.println("count is"+count);

                 if (count==5){
             System.out.println("count is"+count +" and Thread is " +Thread.currentThread().getName());
            // when count is 5 make the current thread should  go to sleep
               Thread.currentThread().sleep(7000);
              }// if count==5 ends here


        //whichever thread starts now , should start the counter from count=5

             if (count==8){
                break;
            // when count is 5 make the current thread go to sleep

              }

          }// while loop ends here
   }// end of counter()
}// end of class ThreadingIst

 public class ThreadTest {

    public static void main(String [] args){

        Thread t1= new Thread(new ThreadingIst());

        Thread t2= new Thread(new ThreadingIst());

        t1.setName("John");
        t2.setName("Gaurav");

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

   }// main ends here

}


输出为:

哪个线程在运行?约翰
计数为1

哪个线程在运行?约翰
计数为2

哪个线程在运行?约翰
数是3

哪个线程在运行?约翰
数是4

哪个线程在运行?约翰
数是5

count is5和Thread Johnis要睡觉了

哪个线程在运行?高拉夫
计数为1

哪个线程在运行?高拉夫
计数为2

哪个线程在运行?高拉夫
数是3

哪个线程在运行?高拉夫
数是4

哪个线程在运行?高拉夫
数是5

count is5和Thread Gauravis入睡

哪个线程在运行?高拉夫
数是6

哪个线程在运行?高拉夫
数是7

哪个线程在运行?高拉夫
数是8

count is8和线程Gauravis退出循环

哪个线程在运行?约翰
数是6

哪个线程在运行?约翰
数是7

哪个线程在运行?约翰
数是8

计数is8和线程约翰尼斯走出循环

我经历了一个答案-"implements Runnable" vs. "extends Thread",其中的注释之一是。但是,实现Runnable和扩展Thread之间的一个重要区别是:
通过扩展Thread,您的每个线程都有一个与之关联的唯一对象,而实现Runnable时,许多线程可以共享同一对象实例。

因此,如果线程可以共享同一对象,则实例值(如count)应由两者共享。那为什么我的输出是这样的。

最佳答案

为了实现这一点,您可以创建两个线程,并让一个线程等待另一个线程,您可以在此处阅读有关加入线程的信息:

http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html

我在想类似的东西:

public class Counter extends Thread {
    private final int countFrom;
    private final int countTo;
    private final Counter counterToWaitFor;

    public Counter(final int countFrom, final int countTo, final Counter counterToWaitFor) {
        super();
        this.countFrom = countFrom;
        this.countTo = countTo;
        this.counterToWaitFor = counterToWaitFor;
    }

    @Override
    public void run() {
        if (this.counterToWaitFor != null) {
            try {
                this.counterToWaitFor.join();
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int i = this.countFrom; i <= this.countTo; i++) {
            System.out.println("i= " + i);
        }
    }
}


在主要方面:

public static void main(final String[] args) throws IOException, InterruptedException {
    final Counter t1 = new Counter(1, 5, null);
    final Counter t2 = new Counter(6, 8, t1);
    t1.start();
    t2.start();
}

10-07 19:01
查看更多