• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant read from other thread

 
Peter Curila
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain me what's wrong with the code.
One thread create class StringQueue where I add some Strings.
Then I try to read from other thread. If queue is empty I wait.
But it does not work.

This is the output when I run the test case:

Time: 0

There was 1 failure:

1) testHelloQueue(StringQueueTestCase)junit.framework.ComparisonFailure: expected:<Ahoj vlakno> but was:<null>

at StringQueueTestCase.testHelloQueue(StringQueueTestCase.java:15)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)



FAILURES!!!

Tests run: 1, Failures: 1, Errors: 0


And here is the code:

 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many ways to fix and many problems with your code so I can only give a hint.

after c.start() put a c.join().

just becuse you call start does not mean the thread will start anytime soon. And ever after it starts, there is no guarantee as to how far it will get before it is stopped by the OS. When you call c.result there is no guarantee if the thread will have even run yet let alone have run enough to have set that variable. You need to use wait(), notify(), a bit of synchronization. Typically the threads run method consists of a state machine and you can call methods on your thread to see what state it is in. Running state, waiting state, exited state, etc...But you have to do that yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!