简体   繁体   中英

Threading issue revealed with Java tutorial on OSX High Sierra

I have been following along with the Java Case Study on cleancoders.com

https://cleancoders.com/videos/java-case-study

You can also find the video series here:

https://www.safaribooksonline.com/videos/clean-code-applied/9780134843810

Round about Episode 4-5, they add some synchronization primitives which for me are just hanging up.

The code can be found here in their github history:

https://github.com/cleancoders/CleanCodeCaseStudy/tree/187e6129de85ad5d33c23ac98a7063b9b35720c5

The commit name is "episode 6 with 3 tomatoes"

I'm using OSX 10.13.6

with Java 1.8

java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

With IntelliJ community edition 2018.2.4

So I'm pretty much stuck now because they move forward based on this code and the tests just hang...

They synchronize on the service object to force events to happen in order:

The test code in question:

@Test
public void canSendAndReceiveData() throws Exception {
   server.start();
   Socket s = new Socket("localhost", port);
   OutputStream os = s.getOutputStream();
   os.write("hello\n".getBytes());
   synchronized(readingService) {
     readingService.wait();
   }
   server.stop(); 

   assertEquals("hello", readingService.message);
 }
}

Then inside the service, they call 'notify()' to release the waiting thread, but it never gets released....

public static abstract class TestSocketService implements SocketService {
 public void serve(Socket s) {
   try {
     doService(s);
     synchronized(this) { notify(); }
     s.close();
   } catch(IOException e) {
     e.printStackTrace();
   }
 }

I'm very stuck ... so any help would be appreciated.

Thanks!

卡住

Additional Note:

If I download the finished project, it has the same problem, so it's clear the tutorial doesn't work through this bug...

https://github.com/cleancoders/CleanCodeCaseStudy

Update

I found a windows computer and installed it there, and it's not showing this problem. So now I might have to conclude it's something to do with this mac I'm using. It's extremely fast:

2.3 GHz Intel Core i5
8 GB 2133 MHz LPDDR3

Maybe it was able to reveal the problem because other computers are slower or maybe it's something to do with the version of Java, anyway, I'm able to continue so, not blocked. I'm going to change the title though to reflect this.

You have:

synchronized(readingService) {
     readingService.wait();

But you're notifying a thread on "this" What you need is to notify the Object reference readingService. What you have is the classic case of notifying the wrong Object.

It's is usually a bad idea to use "this" for wait/notify. It's hard to figure out exactly what "this" refers to. I'm not going to go to github and look at other code.

If you can modify the code, pass a reference to readingService to the Class that implements TestSocketService.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM