Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Synchronization Problem>>>>>>>>

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Threadd implements Runnable
{
public void run()
{
Threadd r1=new Threadd();
r1.loop();
}
synchronized void loop()
{
System.out.println("Thrad is "+Thread.currentThread().getName());
for(int i=0;i<10;i++)
{
System.out.println(" "+i);
}
}
}
public class Thread1syn
{
public static void main(String[] arg)
{
try
{
Threadd r=new Threadd();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.setName("a");
t2.setName("b");
t1.start();
t2.start();
}
catch(Exception e)
{
System.out.println(e);
}
}
}



/* Just to understand synchronization i made this program myself.What's given in K n B , i tried to cover it But program is not behaving like that.when t 1 is executing loo() acquiring the lock of r1 ,t2 should wait to let the lock free.but t2 is also executing concurrently.

****************************
Somebody suggested me to make constructor od class Thredd, i don't know exactly where am i wrong???conceptually or programmatically.
****************************

Please suggest me>>>
Thanks
Deepak
*/
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,

I give you some hint:
1) First of all as per your program, you want there is should be one Object r1 and both thread should acquire lock on that Object. so that each thread print their number from 0 to 9.

2) Both thread will call same run method, as they are created using same instance.

3) Run method is not synchronized, so It may be possible that while running the loop method, Another thread get the CPU cycle and execute the run method.

4) If that another thread executes the run method, it will create one more Object Threadd Object. Again It will refer by r1. This means every time run method is being called, a new Threadd object is being created and On this new Object you are calling the loop method and now you have made the loop method you put synchronized keyword, that mean to say you are putting lock on this object.

5) let come to your code:
suppose thread a runs first, it create r1 Object(hash Code : 1354843), put a lock on that, running the loop method

In between thread b runs, create one new r1 Object(hash Code : 6746521), try to call loop on this new Object, since this new Object is free so acquire lock and run its own loop method.

all you need to do is, Both Object should try to get lock on same object not on different Object.
Now try to correct your code, and get back to JAVARANCH if you face any problem.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, herez what i think:

t1 & t2 are using the same Threadd object "r";
Hence the same run()!

But when t1 & t2 start executing run(),they both execute
Threadd r1=new Threadd();

Hence they create two DIFFERENT objects !! And hence both are basically acquiring two different locks here! So concurrent execution is possible!
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sunny for this nice explanation.It helped me a lot.I'll try to make my code correct...Definitely i'll do it.


And thanks to Mr. Khan for giving this quick response..
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yah , i've got two solutions and could correct my code

1: By making run() synchronized(even i don;t know whether making run() synchronized is right???)

2: by making constructor

class Threadd implements Runnable
{
public synchronized void run()
{
for(int i=0;i<10;i++)
System.out.println(" "+i);
Threadd r1=new Threadd();
r1.loop();
}
synchronized void loop()
{
System.out.println("Thrad is "+Thread.currentThread().getName());
for(int i=0;i<10;i++)
{
System.out.println(" "+i);
}
}
}
public class Threadsyn
{
public static void main(String[] arg)
{
try
{
Threadd r=new Threadd();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.setName("a");
t2.setName("b");
t1.start();
t2.start();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
******************************************************************


class Threadd implements Runnable
{
Caller c;
Threadd(Caller c)
{
this.c=c;
}

public void run()
{

c.loop();
}
}
class Caller
{
synchronized void loop()
{
System.out.println("Thrad is "+Thread.currentThread().getName());
for(int i=0;i<10;i++)
System.out.println(" "+i);
}
}

class Thread1syn
{
public static void main(String[] arg)
{

Caller c1=new Caller();
Threadd o1=new Threadd(c1);
Threadd o2=new Threadd(c1);

Thread t1=new Thread(o1);
Thread t2=new Thread(o2);
t1.setName("a");
t2.setName("b");
t1.start();
t2.start();

}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic