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

Help using threads synch methods

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all
I have a few questions regarding threads.
When creating your thread eg
class MyThread extends Thread
{
public void run()
{
//i want to call other synchronized methods from other objects here which will print something
Test t = new Test();
t.myMethod();//if its the right way to do it
}
public class Test
{
public synchronized myMethod()
{//print something here
//with a for loop x times
}
public static void main(String [] args)
{
MyThread mt = new MyThread();
MyThread mt1 = new MyThread();
MyThread mt2 = new MyThread();
mt.start();
mt1.start();
mt2.start();
}
What will happen if your thread access the myMethod() will it acces the method and print it x times in the loop b4 mt1 start,cos the
method is synch, or will mt1 and mt2 start intermixed.
This is the way i understand synchronized methods the method is locked so the other threads in the que must wait until the first
thread finish, meaning to print out x times b4 the others can enter the method.
I have run a test program ,but the output is mixed especially when the loop count is big, but smaller loops gives me right output.
Thanks for any help
Cornel
 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You will have no control on the order of thread execution. The JVM determines that. Probably if you ran a small loop a number of times, you would eventually see a different result. You can request the thread to have a higher priority though. You are right that only one thread is accessing the synchronized method at a time. Hope this helps!
Barry
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi I tried answering you in this thread.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't post the same question in multiple forums. It wastes other people's time if they don't know what other people have already said on a given topic. Follow up to this question here. Thanks.
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic