Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Is it guaranteed a started thread will run to completion?

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I would like to know that if a thread has been started, it will run to completion?

For example, in this code, will thread "Andi" always print its name?



Is it possible to get this output?

"Eyra ", followed by an exception.

In my opinion this output is not possible. The action in the main thread is sequentially, and the call t1.start() will start thread "Andi". The call t2.start() will throw an IllegalThreadStateException in the main thread because t2 "Eyra" has already started. But after this exception, thread t1 is still alive and it will get the CPU and print "Andi", so the output:

"Eyra ", followed by an exception

is not possible, because thread t1 will ALWAYS print "Andi" as the code stands.

Am I right thinking like that?

Thanks a lot everyone,

Alvaro
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That output is possible:

Thread 2 is started by the method go() and prints Eyra
Thread 1 is started but the scheduler still gives the main thread priority so nothing from the Runnable gets executed.
Thread 2 gets started again by the main thread and throws an exception.
Thread 1 prints Andi

So the output can be

Eyra
Exception
Andi
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer. One thing, as you said:

Thread 2 is started by the method go() and prints Eyra
Thread 1 is started but the scheduler still gives the main thread priority so nothing from the Runnable gets executed.
Thread 2 gets started again by the main thread and throws an exception.
Thread 1 prints Andi

So the output can be

Eyra
Exception
Andi


I am agree with you 100% till this point, but I think the output: "Eyra ", followed by an exception

still needs "Andi" to be correct, or not?

If the output is: "Eyra ", followed by an exception "Andi ", I understand, but if it is: "Eyra ", followed by an exception, I don't really understand.

Other thing is if we say:

The output will contain: "Eyra ", followed by an exception. In this case the option is correct, because it is possible that the output can contain "Eyra " and the exception.

Am I right? I don't know, I really confused.





 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alvaro San Millan wrote:I am agree with you 100% till this point, but I think the output: "Eyra ", followed by an exception

still needs "Andi" to be correct, or not?


Correct.

Alvaro San Millan wrote:
The output will contain: "Eyra ", followed by an exception. In this case the option is correct, because it is possible that the output can contain "Eyra " and the exception.

Am I right? I don't know, I really confused.


Not really. Will contain implies that it happens in that order and always happens which isn't the case. It would consider it "more correct" if it would have said: can be instead of will contain.
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter,

Ok, forget this shade of meaning about the expression "will contain". The important thing for me to understand is:

You are agree with me in that the output: "Eyra ", followed by an exception

still needs "Andi" to be correct.

So, are you agree with me that just the output: "Eyra ", followed by an exception is not possible. It needs "Andi " somewhere in the output.

Am I right?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right.
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic