Created
May 13, 2015 15:21
-
-
Save digvijaybhakuni/6226faa3726346ccb0e5 to your computer and use it in GitHub Desktop.
Learning Syn Block and Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ThreadRunnerClass { | |
public static void main(String[] args) { | |
whenPerInstance(); | |
//whenOneInstance(); | |
} | |
public static void whenPerInstance(){ | |
ShareObjectOne s1 = new ShareObjectOne(); | |
ShareObjectOne s2 = new ShareObjectOne(); | |
new Thread(new MyRunnable(s1), "[Super Hero S1]").start(); | |
new Thread(new MyRunnable(s2), "[Super Hero S2]").start(); | |
} | |
public static void whenOneInstance(){ | |
ShareObjectOne s = new ShareObjectOne(); | |
new Thread(new MyRunnable(s), "[Super Hero S1]").start(); | |
new Thread(new MyRunnable(s), "[Super Hero S2]").start(); | |
} | |
public static class MyRunnable implements Runnable{ | |
ShareObjectOne s; | |
MyRunnable(ShareObjectOne s){ | |
this.s = s; | |
} | |
public void run() { | |
System.out.println(Thread.currentThread().getName() + " started"); | |
s.entryMethod(); | |
/*int s = getIntEnsure(Thread.currentThread().getName().hashCode(),5); | |
int e = getIntEnsure(Thread.currentThread().getName().hashCode(),10); | |
System.out.println(s + " : "+e); | |
this.s.staticEntryMethod(s, e);*/ | |
System.out.println(Thread.currentThread().getName() + " end"); | |
} | |
private int getIntEnsure(int value, int limit){ | |
return value & (limit -1); | |
} | |
} | |
} | |
class ShareObjectOne { | |
/** | |
* | |
*/ | |
public ShareObjectOne() { | |
opiThread = Thread.currentThread().getName(); | |
} | |
public static volatile String opsThread= null; | |
public volatile String opiThread= null; | |
public void entryMethod(){ | |
opiThread = Thread.currentThread().getName(); | |
System.out.println("ENTRING Thread : "+opiThread); | |
try { | |
synMethod(); | |
} catch (InterruptedException e) { | |
System.out.printf("%s thread ki laggai ", opiThread); | |
e.printStackTrace(); | |
} | |
System.out.println("EXITING Thread : "+opiThread); | |
} | |
public synchronized void synMethod() throws InterruptedException{ | |
opsThread = Thread.currentThread().getName(); | |
for(int i =0 ; i < 20; i++){ | |
System.out.printf("%s : %d : %s \n", opiThread , i , opsThread); | |
if("[Super Hero S1]".equals(opiThread)){ | |
opsThread = Thread.currentThread().getName(); | |
if(i%5==0){ | |
mywait(500); | |
}else{ | |
Thread.sleep(100); | |
} | |
}else{ | |
opsThread = Thread.currentThread().getName(); | |
if(i%5==0){ | |
mywait(100); | |
}else{ | |
Thread.sleep(500); | |
} | |
} | |
} | |
} | |
public static void staticEntryMethod(int s, int e){ | |
System.out.println("ENTRING Thread : "+Thread.currentThread().getName()); | |
try { | |
staticSynMethod(s, e); | |
} catch (InterruptedException ex) { | |
System.out.printf("%s thread ki laggai ", Thread.currentThread().getName()); | |
ex.printStackTrace(); | |
} | |
System.out.println("EXITING Thread : "+Thread.currentThread().getName()); | |
} | |
public synchronized static void staticSynMethod(int s, int e) throws InterruptedException{ | |
int ctr = 0; | |
opsThread = Thread.currentThread().getName(); | |
for(int i = s; i < e; i++){ | |
ctr++; | |
if("[Super Hero S1]".equals(opsThread) ){ | |
opsThread = Thread.currentThread().getName(); | |
Thread.sleep(500); | |
MyStaticClas.runnerStatic(200); | |
}else{ | |
opsThread = Thread.currentThread().getName(); | |
Thread.sleep(100); | |
MyStaticClas.runnerStatic(300); | |
} | |
} | |
System.out.println(ctr+" COUNTER MIL GAYA"); | |
} | |
public void mywait(int time) throws InterruptedException{ | |
System.out.println(Thread.currentThread().getName()+" : Waiting for "+time); | |
wait(time); | |
} | |
public void mywait() throws InterruptedException{ | |
System.out.println(Thread.currentThread().getName() + " : Waiting"); | |
wait(); | |
} | |
} | |
class MyStaticClas { | |
public static synchronized void runnerStatic(int sleepTime) throws InterruptedException{ | |
System.out.println(Thread.currentThread().getName()+ " is going to sleep "); | |
Thread.sleep(sleepTime); | |
System.out.println(Thread.currentThread().getName()+ " is going to wake up "); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment