Results 1 to 2 of 2
Thread: Some query about synchronized
- 10-09-2012, 06:01 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 13
- Rep Power
- 0
Some query about synchronized
Java Code:public class SyncTest { public static void main (String[] args) { MysyncTest runna = new MysyncTest(); Thread t1 = new Thread(runna); Thread t2 = new Thread(runna); t1.start(); t2.start(); } } class MysyncTest implements Runnable{ int data = 0 ; int count = 1 ; synchronized public int getData() { data = data + count ; try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return data; } public void setData(int a ) { data = a ; } public void syncBlock() { synchronized (this) { // do something } } @Override public void run() { // TODO Auto-generated method stub ++count; if(count%2 == 0 ) { getData(); } else { setData(count); } } }
Query 1 : Very first time , t1 control will enter in getData() since it is a sync function ,it will acquire lock (object lock ) and this will go to sleep ... Now t2 gets cycle to execute and it will enter in to setData() .. I wanna know since t1 has the lock for the same object how t2 is using that object ??
Query 2 : what is the diff between synchronised function and synchorized(this)
Java Code:synchronized public void fn1(){ } public void fn2(){ synchronized (this) { // Do something here } }
Query 3 : I understood static synchronized , but I dont the know where it can be used .. can anyone help me out ?
- 10-12-2012, 04:18 AM #2
Member
- Join Date
- Jun 2011
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
synchronized(this){}
By zserzs in forum Threads and SynchronizationReplies: 6Last Post: 03-21-2012, 02:08 AM -
synchronized statements query?
By castiel in forum New To JavaReplies: 1Last Post: 02-11-2011, 05:26 PM -
Difference b/w "synchronized","synchronize",and "synchronized()"
By Bala_Rugan in forum New To JavaReplies: 1Last Post: 09-08-2010, 04:08 PM -
Synchronized(this)?
By kiza in forum Threads and SynchronizationReplies: 10Last Post: 04-07-2009, 01:20 PM -
synchronized
By bugger in forum New To JavaReplies: 2Last Post: 11-28-2007, 10:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks