Skip to content

Instantly share code, notes, and snippets.

@liuxiong21
Created September 12, 2014 10:49
Show Gist options
  • Save liuxiong21/586371b634089d1703fd to your computer and use it in GitHub Desktop.
Save liuxiong21/586371b634089d1703fd to your computer and use it in GitHub Desktop.
Java thread同步
package org.freeman.shared;
import java.util.concurrent.*;
/**
* Created by freeman on 14-9-12.
*/
public class ThreadSynchronized {
public static void main(String[] args) throws InterruptedException {
testLatch();
//testCyclic();
//testSemphore();
//testJoin();
//BlockingQueue
//BlockingDeque
}
public static void testLatch() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t1.start();
TimeUnit.SECONDS.sleep(1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t2.start();
System.out.println("Main thread sleep...");
TimeUnit.SECONDS.sleep(10);
System.out.println("open gate");
latch.countDown();
}
public static void testCyclic() throws InterruptedException {
final CyclicBarrier barrier = new CyclicBarrier(2,new Runnable() {
@Override
public void run() {
System.out.println("Await invoked twice on this barrier");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All task thread have been finished");
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t1.start();
TimeUnit.SECONDS.sleep(5);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t2.start();
}
public static void testSemphore() throws InterruptedException {
final Semaphore semaphore = new Semaphore(2);
semaphore.acquire(2);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
semaphore.acquire();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t1.start();
TimeUnit.SECONDS.sleep(1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
semaphore.acquire();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t2.start();
System.out.println("Main thread sleeping...");
TimeUnit.SECONDS.sleep(10);
System.out.println("Main thread release 2");
semaphore.release(2);
}
public static void testJoin() throws InterruptedException {
final Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" sleeping...");
TimeUnit.SECONDS.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t1.start();
TimeUnit.SECONDS.sleep(1);
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("thread "+Thread.currentThread().getId()+" waiting...");
t1.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("thread "+Thread.currentThread().getId()+" do something...");
}
});
t2.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment