• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads Using Static Method

 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeap.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[B] tags ?[/B]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic