Created
March 22, 2019 11:45
-
-
Save yeshengwu/4dbf837e5473ba7ae635797e0629db43 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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