Last active
January 22, 2017 14:21
-
-
Save umbertogriffo/30339a37b83303de0803c5719161617d to your computer and use it in GitHub Desktop.
How to make the method run() of class NoThreadSafe thread-safe in Java
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 Method1 { | |
/* | |
Adding synchronized to this method will makes it thread-safe. | |
When synchronized is added to a static method, the Class object is the object which is locked. | |
*/ | |
public static void main(String[] args) throws InterruptedException { | |
ProcessingThreadS pt = new ProcessingThreadS(); | |
Thread t1 = new Thread(pt, "t1"); | |
t1.start(); | |
Thread t2 = new Thread(pt, "t2"); | |
t2.start(); | |
// wait for threads to finish processing | |
t1.join(); | |
t2.join(); | |
System.out.println("Processing count=" + pt.getCount()); | |
} | |
} | |
class ProcessingThreadS implements Runnable { | |
private int count; | |
@Override | |
public void run() { | |
for (int i = 1; i < 5; i++) { | |
processSomething(i); | |
synchronized(this){ | |
this.count++; | |
} | |
} | |
} | |
public synchronized int getCount() { | |
return this.count; | |
} | |
private void processSomething(int i) { | |
// processing some job | |
try { | |
Thread.sleep(i * 1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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 Method2 { | |
/* | |
We can make count++ atomic by using AtomicInteger from the package "java.util.concurrent.atomic" | |
*/ | |
public static void main(String[] args) throws InterruptedException { | |
ProcessingThreadS2 pt = new ProcessingThreadS2(); | |
Thread t1 = new Thread(pt, "t1"); | |
t1.start(); | |
Thread t2 = new Thread(pt, "t2"); | |
t2.start(); | |
// wait for threads to finish processing | |
t1.join(); | |
t2.join(); | |
System.out.println("Processing count=" + pt.getCount()); | |
} | |
} | |
class ProcessingThreadS2 implements Runnable { | |
private static AtomicInteger count = new AtomicInteger(0); | |
@Override | |
public void run() { | |
for (int i = 1; i < 5; i++) { | |
processSomething(i); | |
count.getAndIncrement(); | |
} | |
} | |
public int getCount() { | |
return count.get(); | |
} | |
private void processSomething(int i) { | |
// processing some job | |
try { | |
Thread.sleep(i * 1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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 NoThreadSafe { | |
public static void main(String[] args) throws InterruptedException { | |
ProcessingThread pt = new ProcessingThread(); | |
Thread t1 = new Thread(pt, "t1"); | |
t1.start(); | |
Thread t2 = new Thread(pt, "t2"); | |
t2.start(); | |
// wait for threads to finish processing | |
t1.join(); | |
t2.join(); | |
System.out.println("Processing count=" + pt.getCount()); | |
} | |
} | |
class ProcessingThread implements Runnable { | |
private int count; | |
@Override | |
public void run() { | |
for (int i = 1; i < 5; i++) { | |
processSomething(i); | |
count++; | |
} | |
} | |
public int getCount() { | |
return this.count; | |
} | |
private void processSomething(int i) { | |
// processing some job | |
try { | |
Thread.sleep(i * 1000); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment