Forums Register Login

Doubt with a thread question

+Pie Number of slices to send: Send
Hello friends, can anybody explain me the behavior of the threads in this exercise?


Best Regards
+Pie Number of slices to send: Send
What is it doing, and what is confusing you about it?
+Pie Number of slices to send: Send
Couple of additional things, besides you needing to TellTheDetails(⇐click) as already mentioned.

1. When posting code, UseCodeTags(⇐click) so it will be readable.

2. I don't know if you wrote that code yourself or if it came from a book or website, but that's a horrible way to create a thread. Don't use that pattern for real code. It would be better to create a separate class that implements Runnable. Extending Thread is bad enough, but to do it with the main class, and then pass that Thread subclass to a normal Thread as its Runnable is just a mess, and will lead to confusion.
+Pie Number of slices to send: Send
Sorry for not being clear before.

It is an excercise for the scjp exam, I want to know why only two threads are involved? the possible answers are

1- The output could be Thread-1 Thread-3 Thread-1 Thread-2
2- The output could be Thread-1 Thread-3 Thread-1 Thread-3
3- The output could be Thread-1 Thread-1 Thread-2 Thread-2
4- The output could be Thread-1 Thread-3 Thread-3 Thread-1
5- The output could be Thread-1 Thread-3 Thread-1 Thread-1

But I dont understand why the number one it is not a correct answer.
+Pie Number of slices to send: Send
 

Cheo Gomez wrote:I want to know why only two threads are involved?


Only two Threads ever get started. The fact that the two Fabric objects happen to be Threads as well is irrelevant. They're just being used as Runnables (which is valid because Thread implements Runnable). But that code never calls start() on them.
+Pie Number of slices to send: Send
Im still confuse because I saw the start method calling t and t2, and the main thread is running too.
+Pie Number of slices to send: Send
 

Cheo Gomez wrote:Im still confuse because I saw the start method calling t and t2, and the main thread is running too.



The main thread's run method doesn't call that loop in your code. It basically just calls main(). Only Fabrics will run that loop, and there are only 2 Fabrics.

If the code were written more properly, it would look something like this:



The above is functionally equivalent to your original code. Is it easier to understand the behavior now?
+Pie Number of slices to send: Send
Thank you guys very much, I understand now.



Jeff Verdegan wrote:

Cheo Gomez wrote:Im still confuse because I saw the start method calling t and t2, and the main thread is running too.



The main thread's run method doesn't call that loop in your code. It basically just calls main(). Only Fabrics will run that loop, and there are only 2 Fabrics.

 
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1001 times.
Similar Threads
Clarify Use of the join method
Thread Priority issue
Collections/HashMap and threads
Threads
wait() and notifyAll()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:42:32.