This week's giveaway is in the OCPJP forum.
We're giving away four copies of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) and have Paul Anilprem on-line!
See this thread for details.
Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Question about Thread.

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class CounterThread extends Thread
{
public static void main(String[] args)
{
CounterThread ct=new CounterThread();
ct.start();
ct.start();
}
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
Hi guys,just think what might be the answer to the above question.Is it
a)Compile time error.
b)The code executes the print statement and prints twice from 1 to 10.
c)Run time exception.
d)Run time exception followed by the print statement from 1 to 10.
e)None of the above.
Just take a guess.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Answer is (d).
Thanks.
/Nirmal
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surya,
I think its d.None
It's gives you IllegalThreadState Exception.
Once thread is dead it cannot be restarted.
Regards,
Vijay
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The answer is b). Prints twice the numbers 1 to 10.
This is a trick question. Eventhough the class extends Thread, it never creates a new thread. As it extends Thread, it inherits start() method, and thru which calls run() method.
The whole thimg executes in main thread.
cheers
Sankar
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i ran the code & it printed 1 to 10 once also gave IllegalThreadStateException.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It gives IllegalThreadStateException.. why is that so?
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
The example was picked from RHE and i just changed the code to call the start method twice.By the way the answer is D i.e
Run time exception followed by the print statement from 1 to 10.
From RHE:
When the run() method returns the thread has finished its task and considered dead,Once a thread is dead it may not be started again,if you want the thread's task to be performed again you have to construct and start a new thread instance.The dead thread continues to exist and you can still access its data and call its methods .You can't make it run again.In other words :
a)You can't restart a dead thread
b)You can call the methods of a dead thread.
The IllegalThreadStateException is thrown to indicate that a thread is not in an appropriate state for the requested operation.
Have fun.Bye.
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The fun Continues.....
On my machine, it prints an error message, prints 0, another error message, and continues the loop to 9.
A point to note:
ct.start();
ct.start();
It is true that a dead thread can not be restarted.But in the above snippet when the first thread is created, the second statement does not wait for the first thread to die before it attempts its call.The whole point of multithreading is that we should be able to execute other code while other code is also executing concurrently.So while the first thread is still in its loop, we attempt the second call and depending on the system, the error can be generated at any time, could be after the first one dies, or is still alive.May be that can explain my result.
Regards,
herbert
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason you get differing results is that there is no guarantee about when the Thread will actually run after being started, so if the main Thread continues, the second start() will be called before the run method is excuted.
It all depends on how the underlying JVM schedules threads.
Since the new Thread inherits the priority from the creating thread, they will both have the same priority.
 
Surya B
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Herbert Maosa
Thanks for pointing it out .Can you please tell me as to what OS and Java version are you using.The RHE says something about
Preemptive scheduling(Solaris)
In preemptive scheduling in order for the thread to leave the
Running state without explicitly calling a thread-scheduling method (wait,suspend) it has to:
a)Cease to be Ready to execute(Blocking IO method for example)
b)Moved out of the CPU by a higher priority thread that becomes ready to execute.
Time-sliced or round-robin scheduling.(Macintosh)
a)A thread is allowed to execute only for a limited amount of time,and is moved to the Ready state.
b)It ensures against that a single Higher priority thread getting into the running state and never getting out.
I think the behaviour of the program has got to do with the implementation of the thread schedulers.What do you say???
Surya
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Mac OS 8.6 and Apple MRJ 2.2 (JDK 1.1.8) and I get answer (d):
java.lang.IllegalThreadStateException
java.lang.IllegalThreadStateException
at CounterThread.main(CounterThread.java:5)
1
2
3
4
5
6
7
8
9
10
Steve
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This means that once the run method has finished executing the thread is dead.i.e u need not call the threads dead method explicitly to be dead is it so
Am i correct
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Indu mathy:
This means that once the run method has finished executing the thread is dead.i.e u need not call the threads dead method explicitly to be dead is it so
Am i correct


Run this example multiple times....
Sometimes prints 1-10 twice and
Sometimes throws an IllegalThreadStateException
The true answer comes from the schedular. If the schedular trys to start the thread the second time before the first is finished running, IllegalThreadStateException adding the line
while(ct.isAlive());
between the two starts and test it.
It prints 1-10 only once without any Exceptions.
This is because the thread is dead and cannot be restarted. So, a thread actually (and for a short time) must enter a ready state before entering the dead state and if the second start() is recieved by the scedular at that time, it happily restarts it.

[This message has been edited by Carl Trusiak (edited June 24, 2000).]
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
This question has nothing to do with trying to re-start a dead thread.
The output I'm getting is an IllegalThreadStateException followed by a print of 1 to 10.
Clearly the second start() call is made before the first run() method has returned. (You can even test this by inserting a sleep in your run() method).
The API simply says that the start() method throws an IllegalThreadStateException if the thread was already started. That is calling start() twice on the same thread instance will throw this run-time exception. As simple as that!
 
reply
    Bookmark Topic Watch Topic
  • New Topic