gpt4 book ai didi

java - 一个循环如何由两个线程完成?说由 ist 线程从 count=1 循环到 count=4 并由第二个线程循环 count =5 到 8?

转载 作者:行者123 更新时间:2023-12-02 05:01:34 27 4
gpt4 key购买 nike

我想要的输出的描述:我想要两个线程 Gauravjohn 完成一个 while 循环(从 1 到 8),这样无论哪个线程启动 ist,都会运行 5 次迭代(即直到 count=5 ) ,然后进入休眠状态,然后下一个线程完成循环(从 count=6 运行到 count=8)。程序应该结束。对于这种情况,我创建了一个实例变量计数并在计数器方法中增加其值

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

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

计数为 5,线程 Johnis 即将 hibernate

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

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

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

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

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

计数为5,线程Gauravis 即将 hibernate

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

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

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

计数为8且线程Gauravis退出循环

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

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

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

计数为8且线程Johnis脱离循环

我已经浏览了一个答案 - "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();
}

关于java - 一个循环如何由两个线程完成?说由 ist 线程从 count=1 循环到 count=4 并由第二个线程循环 count =5 到 8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28236943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com