This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Using Static Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Using Static Method" Watch "Threads Using Static Method" New topic
Author

Threads Using Static Method

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
public class Test1 extends Thread
{
public void run(){
display();
}
synchronized static void display(){//synchronized static
for(int i=0;i<7;i++){
System.out.println(Thread.currentThread().getName()+" "+i+" ");
}
System.out.println("--------");
}


public static void main(String s[]){
Test1 t1=new Test1();
Test1 t2=new Test1();
Test1 t3=new Test1();
Test1 t4=new Test1();
Test1 t5=new Test1();
t1.setName("T1: ");
t2.setName("T2: ");
t3.setName("T3: ");
t4.setName("T4: ");
t5.setName("T5: ");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();

System.out.println("Main Thread Ended");
System.out.println("-----------------");
}
}
In above program there are 5 instances of Test1 (extends Thread) class. All 5 threads are accessing Static method name display.

Now my question is that �do all threads (t1, t2, t3, t4, t5) in above program shares single copy of
static display method�.

My another question is that �if I replace code shown below with code in main in above program The result will be still same�.

Test1 t1=new Test1();
Thread d1=new Thread(t1);
Thread d2=new Thread(t1);
Thread d3=new Thread(t1);
Thread d4=new Thread(t1);
d1.start();
d2.start();
d3.start();
d4.start();

Thanks
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Yeap.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
[B] tags ?[/B]


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
1) Yes.
2) The result will be "similar", not same.

In the first version, you are explicitly setting the names. In the second version, the names will be given automatically.

In the first version you are running five threads from main, in the second version you are running four.

As far as the output it is guaranteed that once a thread starts executing "display" method, no other thread can start it unless the first finishes. However, the relative ordering between the threads will still be at the JVM's discretion.

Also, there is a "main thread" which is printing "Main Thread Ended" and "-----------------" . This is independent of the display method, and so these two statements will be interwoven with the output in the random manner.

Essentially, the static method 'display' will still be shared and since the static method is synchronized, only one thread will be able to execute it at a time.
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Now my question is that �do all threads (t1, t2, t3, t4, t5) in above program shares single copy of
static display method�.


Yes. All share the same copy.

My another question is that �if I replace code shown below with code in main in above program The result will be still same�.


You will get the similar output. Thread d1=new Thread(t1); is just another way of starting a thread.


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
Great work, Neelesh!

I couldn't have explained it better.

Another question for new Java learners (profis, please resist...):

When we are dealing with a static method, what object gets locked in order to achieve synchronization? How does the JVM know not to let other threads access synchronized static methods (in this case, the display() method.)?
[ July 21, 2006: Message edited by: Douglas Chorpita ]

SCJP 1.4 - 95%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Using Static Method
 
Similar Threads
Thread synchronisation - Using a string literal for locking
Thread functioning help
Synchronization of run method
Synchronozation How it Works
ThreadUnsafe