Created
May 15, 2013 14:53
-
-
Save zynick/5584589 to your computer and use it in GitHub Desktop.
concurrent test sample
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 test.thread; | |
import java.util.ArrayList; | |
public class ConcurrentTest { | |
public static void main(String[] args) throws Exception { | |
new ConcurrentTest().execute(); | |
} | |
public void execute() throws Exception { | |
ArrayList<TestThread> list = new ArrayList<TestThread>(); | |
Object lock = new Object(); | |
for (int i = 0; i < 10; i++) { | |
TestThread t = new TestThread(lock); | |
t.start(); | |
list.add(t); | |
} | |
Thread.sleep(500); | |
synchronized(lock) { | |
lock.notifyAll(); | |
} | |
} | |
public class TestThread extends Thread { | |
private Object lock; | |
public TestThread(Object lock) { | |
this.lock = lock; | |
} | |
public void run() { | |
try { | |
synchronized (lock) { | |
System.out.println(this.getName() + " wait()"); | |
lock.wait(); | |
} | |
System.out.println(this.getName() + " run()"); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment