• 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

some problems regd threads as in velmurugan's notes

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.)Murugan says:
//. The main method of the class is called from the main thread. It dies when the main method ends. If other user threads have been spawned from the main thread, program keeps running even if main thread dies. //
while patrick naughton says that the main thread is always the last thread to terminate. the very purpose of join() is to wait for the child threads to terminate, b4 terminating itself. Please comment...
2.)//�A single thread can obtain multiple locks on multiple objects (or on the same object)//
multiple locks on one object?? he means the same object class ,right? like:
//code
class a{
synchronized method1(){
a a1 = new a();
a1.method2();
//code
}
synchronized method2(){
//code
}
}
//code finished
i mean, an object has a single lock, right?
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesn't anybody have anything to comment on this???
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Yes, anshuman i think what u said is correct. Main thread will die after all other threads.

------------------
SeE Consulting(P) Ltd
Bangalore,India
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshuman,
The main thread is not always the last thread to die. If you run the code below you will see the last print statement in main is printed before the last thread dies.
public class ThreadTester {
public static void main( String args[] )
{
PrintThread thread1, thread2, thread3, thread4;
thread1 = new PrintThread( "thread1" );
thread2 = new PrintThread( "thread2" );
thread3 = new PrintThread( "thread3" );
thread4 = new PrintThread( "thread4" );
System.err.println( "\nStarting threads" );
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.err.println( "Threads started\n" );
}
}
class PrintThread extends Thread {
private int sleepTime;
// PrintThread constructor assigns name to thread
// by calling Thread constructor
public PrintThread( String name )
{
super( name );
// sleep between 0 and 5 seconds
sleepTime = (int) ( Math.random() * 5000 );
System.err.println( "Name: " + getName() +
"; sleep: " + sleepTime );
}
// execute the thread
public void run()
{
// put thread to sleep for a random interval
try {
System.err.println( getName() + " going to sleep" );
Thread.sleep( sleepTime );
}
catch ( InterruptedException exception ) {
System.err.println( exception.toString() );
}
// print thread name
System.err.println( getName() + " done sleeping" );
}
}
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anshuman,
I do agree with both of you that main thread dies after all threads.
The main thread which is the Java Virtual Machine own thread.
This is used for the so-called event system, which handles user input such as keyboard events, mouse pointer movements. The problem with other thread is that like animations ok, is that
they often requires that you have a thread that is constantly
working with the animation code. We cannot use the main thread
because then the applet's other functions will stop working, the
applet hangs. A program that hangs, usually does that because the main thread gets stuck in an program loop and cannot get free, or that it collides with another thread and stops.
But here another question is being raised as per Mr. Murugan's definition about the main Thread.
He is saying that "If other user threads have been spawned from the main thread, program keeps running even if main thread dies."
But my point is that if we need another thread like animation thread. Here we can not use main thread because its busy with
other JVM's user inputs. So if main thread dies when animation thread is on running then applet would be hanged. So when applet hanged then how come animation thread can be worked.
Thanks,
Golam Newaz
 
Anshuman Acharya
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i checked the case with a very simple example of creating a frame via another thread in a main method.
i think that the main thread is the last one to terminate, that is, what Naughton says.
As for your example Kevin.. the printing of the last print statement proves nothing. the main thread then waits after the printing for the child threads to die...
printing the last statement doesn't say that the party is over yet...
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here are my $0.02 on the topic:
I checked my results with simple test codes.
1)If the main thread spawns some non-daemon threads, then the main thread waits for the other threads to exit before exiting, i.e. main thread is the last thread to terminate. It probably does this via join(). So Naughton seems to be right on this one (he should know --- he is one of the original creators of the language!).
2)If this spawned thread (parent)also spawns another thread (child), then the parent does NOT wait for the child to finish. i.e. if the parent thread has finished it's work, it exits even though child may be running. Thus the contrary is true only in the case of main thread.
3)Main thread does not respond to keyboard or mouse events (that is the work of the event-dispatch thread). In any GUI program there are several other threads like GUI thread etc, which do the work of painting & responding to events. All of the event-handling & painting of the GUI is done on the event-dispatch thread. Thus if you write some long time-consuming code in your ActionListeners, don't be surprised if your GUI freezes up and fails to respond to mouse-clicks!
4)If a thread already has the lock of some object, then it can re-acquire the same lock, otherwise it would lead to a deadlock. This is more commonly known as re-entrant locks (I think)
Hope this helps in clearing up the confusion!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So there is only one lock per object right?. A thread which has already acquired a lock on an object can call any other syncrhonized method of the object. May be this is what you mean by "Re-entrant" lock?.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Junaid Bhatra:
Ok here are my $0.02 on the topic:
I checked my results with simple test codes.
...
Hope this helps in clearing up the confusion!


---------------
Check out absolutejava.com for a good explanation of this. http://www.absolutejava.com/articles/beware-the-daemons.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic