Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Concurency logic

Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, this is my first post here.

The subject is about threads that are started and changed their name. I'm interested only in the way of reading the code and after the that the correct answer.
Here is the code (ETS questions).

In my logic the following code evolves like this:

1. The statement : Thread.currentThread().setName("First"); in the main() method sets the name of the current thread. This is the thread that is running the main method.
(ETS also explanation) - i'm ok with this

2. The statement: MyRunnable mr = new MyRunnable("MyRunnable") is going to create new MyRunnable object.
But in it's creation process by running it's constructor, it creates a new thread with the name "MyRunnable" and also starts this thread. When it starts it then the newly created thread with the name "MyRunnable" will access the method run() from the "My Runnable" class.
Let's say that the scheduler allows the newly created thread with the name "My Runnable" to actually run and thus it will print the name "My Runnable".
After this, the new MyRunnable is created (it's constructor compleets running).

3. the following method of the objet MyRunnable: "" is executed. The object "mr" is not a thread is a simple object with the method run().
Now... this method executes the code "Thread.currentThread().getName()" whitch gets the current thread name. I suppose that the current thread is main() because the method is invoked from within the method main and the code "Thread.currentThread().getName()" is accessed by run() method of the mr objet.
After that it will print "First" .

4. Thread.currentThread().setName("Second"); - the main thread changes the name from "First" to "Second".
Again is invoked the method run() of the mr object and this time it prints "Second"

If the scheduler decides to run the new thread "MyRunnable" then in my opinion the printout will be this:
If the scheduler decides not to run at that moment the new thread "MyRunnable" then "MyRunnable" could be placed anyware after the first "First" :

In the ETS response option:
-It will always print: First, MyRunnable, Second.
-It will always print: MyRunnable, First, Second.
-It will always print: First, Second, MyRunnable.
-It may print First, Second and MyRunnable is any order.
-Second will never be printed before First.

The correct answer is (by ETS)
-Second will never be printed before First.

Please tell if i'm reading this code correctly. And if not where is the mistake of my logic.

Best regards.

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i have understood you understanding is correcrt
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Andi Dan, welcome to Javaranch.

Could you put your code wrapped by the code tag? It will be easier to us to help you.

Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your understanding concurs with ets provided answer. Otherwise which other answer do you suggest?
I found some pretty shells, some sea glass and this lovely tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic