Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeshengwu/4dbf837e5473ba7ae635797e0629db43 to your computer and use it in GitHub Desktop.
Save yeshengwu/4dbf837e5473ba7ae635797e0629db43 to your computer and use it in GitHub Desktop.
public class TestSync {
public synchronized void run1() {
for (int i = 0; i < 10; i++) {
System.out.println("execute run1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void run2() {
for (int i = 0; i < 10; i++) {
System.out.println("execute run2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
final TestSync testSync = new TestSync();
Thread thread1 = new Thread(new Runnable()
{
@Override
public void run()
{
testSync.run1();
}
});
Thread thread2 = new Thread(new Runnable()
{
@Override
public void run()
{
testSync.run2();
}
});
thread1.start();
thread2.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment