The code snippet below is in my JUnit Test Case class. I am using three threads to test a class called SharedResources; getGuy is just one of them. My problem is after starting the threads, only the first one reads the sharedResource, and that only once.
下面的代码片段在我的JUnit Test Case类中。我使用三个线程来测试一个名为SharedResources的类; getGuy只是其中之一。我的问题是在启动线程后,只有第一个读取sharedResource,而且只读取一次。
Thread getGuy = new Thread(new Runnable() {
public void run() {
for(int i=0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I consume",resource.get());
}//for
}
});
Thanks for any help.
谢谢你的帮助。
1 个解决方案
#1
0
This is a basic producer-consumer design. Instead of creating producer and consumer classes, I simply create the sharedResource class. Then in the JUnit Test Case, I create the producer and consumer Threads. getGuy is a consumer thread.
这是一个基本的生产者 - 消费者设计。我只是创建sharedResource类,而不是创建生产者和消费者类。然后在JUnit测试用例中,我创建了生产者和消费者线程。 getGuy是一个消费者线程。
Thanks
#1
0
This is a basic producer-consumer design. Instead of creating producer and consumer classes, I simply create the sharedResource class. Then in the JUnit Test Case, I create the producer and consumer Threads. getGuy is a consumer thread.
这是一个基本的生产者 - 消费者设计。我只是创建sharedResource类,而不是创建生产者和消费者类。然后在JUnit测试用例中,我创建了生产者和消费者线程。 getGuy是一个消费者线程。
Thanks