Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Using Static Method

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeap.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B] tags ?[/B]
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic