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.
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.
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 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 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.
Post by:autobot
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, the permaculture playing cards