Skip to content

Instantly share code, notes, and snippets.

@zynick
Created May 15, 2013 14:53
Show Gist options
  • Save zynick/5584589 to your computer and use it in GitHub Desktop.
Save zynick/5584589 to your computer and use it in GitHub Desktop.
concurrent test sample
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