wood burning stoves 2.0*
The moose likes Java in General and the fly likes Doubt with a thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt with a thread question" Watch "Doubt with a thread question" New topic
Author

Doubt with a thread question

Cheo Gomez
Greenhorn

Joined: Jan 24, 2012
Posts: 25
Hello friends, can anybody explain me the behavior of the threads in this exercise?


Best Regards
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

What is it doing, and what is confusing you about it?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Cheo Gomez
Greenhorn

Joined: Jan 24, 2012
Posts: 25
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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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.
Cheo Gomez
Greenhorn

Joined: Jan 24, 2012
Posts: 25
Im still confuse because I saw the start method calling t and t2, and the main thread is running too.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
Cheo Gomez
Greenhorn

Joined: Jan 24, 2012
Posts: 25
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt with a thread question
 
Similar Threads
wait() and notifyAll()
Threads
Thread Priority issue
Collections/HashMap and threads
Clarify Use of the join method