• 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

Thread synchronization question ?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear ranchers,
I have this question for a JQPlus mock. Hope someone can throw more light on this one

The output I thought was right is
Numbers 1 to 5 will be printed (none repeated or missed) in any order followed by "N Done" where N cannot be determined.
Since it is synchornized, this should be the right one.

But the correct output is
Numbers 1 to 5 will be printed (some may be repeated or missed) with "N Done" anywhere in the sequence where N cannot be determined.

The reason is that there are 25 different TestClass objects and each thread gets a separate lock on the object...but I am not very sure...any ideas??

thanks,
Roopesh.
[ September 12, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say
for(i=0;i<5;i++)
new TestClass().start(); you are creating five different objects

there are five TestClass objects,
every object in java can have one monitor or lock,
and once you obtain lock on an object no other thread can enter synchronized method of that object but here there are five TestClass object and five threads are started each one has individual identity.


say you say,

new TestClass().start();

// now consider it as thread 1 object TestClass 1
// now when you say,

new TestClass().start();

// now it is new TestClass object and another Thread (or different thread)
// it is thread 2 object TestClass 2

so even first TestClass object thread 1 enters synchronized code by obtaining lock of TestClass object 1 the next thread 2 can enter synchronized code as it obtains lock on its TestClass object different than first object.

may be you can do this thing

for(i=0;i<5;i++)
{
TestClass a = new TestClass();
a.setName("Thread " + i);
a.start();
}

and inside run method
give code
System.out.print(Thread.currentThread().getName());

you will see different thread names.
[ September 12, 2005: Message edited by: Santana Iyer ]
 
Roopesh Gulecha
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sanatana,
That surely helps..I see the point now - so the following code


is different from the one that was mentioned earlier. Here 25 different threads are competing to obtain the lock for a single ThreadClass object, whereas the earlier code there are 25 different ThreadClass objects.
Am I right?

Roopesh.
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes see you specify target that is object implements Runnable
so still there are many threads but target is one
so if run method is synchronized than only one thread can enter
method as it must obtain lock on object.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sanatana ,

why is then the main thread's threadcounter is printing 0??
i think iam losing some in my basics.please help.

srikanth
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look main is also a thread so it is possible that it gets executed before other thread.

example

class xyz extends Thread
{
String name;

public xyz(String s)
{
name=s;
}

public void run()
{
System.out.print(name);
}

public static void main(String args[]) throws Exception
{
Thread t=new xyz("First");
Thread t1=new xyz("Second");
t.start(); // 1
// here
t1.start(); // 2
// some
System.out.print("Third");
}
}
program prints three words First Second Third but
order is not guaranteed for First Second Third,
because when you start t and t1 main thread is also running it is possible that main thread runs and completes before t or t1 and printing
Third First Second or Third Second First (depending on which t ot t1 runs first).

but if you want to be sure that it always prints First Second Third in that order do following

replace // here with t.join();
replace // some with t1.join();

Output is FirstSecondThird // guaranteed always

join is join thread that executes this code (main here) to end and so when you say t.join() Thread t will complete first and only than main will start.

now first t thread will run to completion printing First than t1 Seond and main thread runs Third will be printed last always.
[ September 12, 2005: Message edited by: Santana Iyer ]
 
This will take every ounce of my mental strength! All for a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic