Created
April 12, 2014 09:11
-
-
Save rajeevprasanna/10526186 to your computer and use it in GitHub Desktop.
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
package example7; | |
import java.util.Collections; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class NameListFailing { | |
private List names = Collections.synchronizedList(new LinkedList()); | |
public void add(String name) { | |
names.add(name); | |
} | |
//This code can break if we run the program multiple times | |
//Even collection.syncronize also can fail when dealing with multiple threads if we don't code properly. | |
//There's a solution here: don't rely on Collections.synchronizedList(). Instead, synchronize the code yourself: | |
public String removeFirst() { | |
if (names.size() > 0) | |
return (String) names.remove(0); | |
else | |
return null; | |
} | |
public static void main(String[] args) { | |
final NameListFailing nl = new NameListFailing(); | |
nl.add("Ozymandias"); | |
class NameDropper extends Thread { | |
public void run() { | |
String name = nl.removeFirst(); | |
System.out.println(name); | |
} | |
} | |
Thread t1 = new NameDropper(); | |
Thread t2 = new NameDropper(); | |
t1.start(); | |
t2.start(); | |
} | |
} |
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
package example7; | |
public class SyncTest { | |
public void doStuff() { | |
System.out.println("not synchronized"); | |
synchronized (this) { | |
System.out.println("synchronized"); | |
} | |
} | |
public synchronized void doStuff1() { | |
System.out.println("synchronized"); | |
} | |
// doStuff1 is equivalent to this: | |
public void doStuff3() { | |
synchronized (this) { | |
System.out.println("synchronized"); | |
} | |
} | |
public static void getCount() { | |
synchronized (SyncTest.class) { | |
System.out.println("synchronized static method"); | |
} | |
} | |
public static void classMethod() throws ClassNotFoundException { | |
Class cl = Class.forName("SyncTest"); | |
synchronized (cl) { | |
// do stuff | |
} | |
} | |
} |
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
package example7; | |
public class Thing { | |
private static int staticField; | |
private int nonstaticField; | |
public static synchronized int getStaticField() { | |
return staticField; | |
} | |
public static synchronized void setStaticField(int staticField) { | |
Thing.staticField = staticField; | |
} | |
public synchronized int getNonstaticField() { | |
return nonstaticField; | |
} | |
public synchronized void setNonstaticField(int nonstaticField) { | |
this.nonstaticField = nonstaticField; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment