• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with a thread question

 
Cheo Gomez
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends, can anybody explain me the behavior of the threads in this exercise?


Best Regards
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it doing, and what is confusing you about it?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im still confuse because I saw the start method calling t and t2, and the main thread is running too.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic