开发者

Threads inside JUnit Test Case to test SharedResource class

开发者 https://www.devze.com 2023-03-28 09:40 出处:网络
The code snippet below is开发者_C百科 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

The code snippet below is开发者_C百科 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.

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.


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.

Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消