Java线程同步 2
2008-10-01 20:07
543 查看
CyclicBarrier用于设置一个Barrier,达到Barrier的线程将等待,当所有线程都达到Barrier时,所有线程继续执行。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1. 创建CyclicBarrier时通过构造函数指定线程的数量n。
2. 每个线程调用await方法等待其他线程。
3. 当有n个await方法被调用时,所有处于await的线程都将继续执行。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args)
{
// 初始化
barrier = new CyclicBarrier(10);
// 启动10个线程
System.out.println("main: starting 10 threads.");
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
try
{
// 随机等待5-14秒
int n = (int)(Math.random() * 10 + 5);
System.out.printf("%s: sleep for %d seconds. /n", thName, n);
Thread.sleep(n * 1000);
// 等待其他线程
System.out.printf("%s: waiting for other threads. /n", thName);
barrier.await();
// 结束
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
return;
}
catch (BrokenBarrierException e)
{
System.out.printf("%s: broken barrier. /n", thName);
return;
}
}
}
}
1. 在调用await方法时设置超时时间。
2. 当有一个await方法超时,所有处于await等待的线程将收到BrokenBarrierException,超时的线程自身将收到TimeoutException。
3. BrokenBarrier之后的所有await调用将直接抛出BrokenBarrierException。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class CyclicBarrierBrokenTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args)
{
// 初始化
barrier = new CyclicBarrier(10);
// 启动10个线程
System.out.println("main: starting 10 threads.");
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
try
{
// 随机等待5-14秒
int n = (int)(Math.random() * 10 + 5);
System.out.printf("%s: sleep for %d seconds. /n", thName, n);
Thread.sleep(n * 1000);
// 等待其他线程
System.out.printf("%s: waiting for other threads. /n", thName);
barrier.await(5, TimeUnit.SECONDS);
// 结束
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
return;
}
catch (BrokenBarrierException e)
{
System.out.printf("%s: broken barrier. /n", thName);
return;
}
catch (TimeoutException e)
{
System.out.printf("%s: timeout. /n", thName);
return;
}
}
}
}
当调用await线程达到CyclicBarrier的设置之后,CyclicBarrier将被重置,重新等待n个await调用。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierResetTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args) throws Exception
{
barrier = new CyclicBarrier(2);
Thread th1 = new Thread(new TestRunnable(), "thread 1");
Thread th2 = new Thread(new TestRunnable(), "thread 2");
th1.start();
th2.start();
Thread.sleep(60 * 1000);
th1.interrupt();
th2.interrupt();
th1.join();
th2.join();
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
System.out.printf("%s: start. /n", thName);
try
{
while (true)
{
// 随机产生1-100的整数。
int c = (int)(Math.random() * 100) + 1;
// 随机等待0-4秒。
int t = (int)(Math.random() * 5);
System.out.printf("%s: sleep for %d seconds./n", thName, t);
Thread.sleep(t * 1000);
// 等待另1线程。
barrier.await();
// 输出产生的整数。
System.out.printf("%s: %d /n", thName, c);
// 等待其他线程输出。
Thread.sleep(100);
}
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
}
catch (BrokenBarrierException e)
{
e.printStackTrace();
}
System.out.printf("%s: end. /n", thName);
}
}
}
1. 在创建CyclicBarrier的时候指定通过Barrier时需要执行的语句。
2. 通过构造方法的Runnable参数指定。
3. Runnable中的语句将由最后达到Barrier的线程执行。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierActionTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args) throws Exception
{
barrier = new CyclicBarrier(10, new BarrierAction());
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
try
{
String thName = Thread.currentThread().getName();
// System.out.printf("%s: start. /n", thName);
// 随机等待0-9秒
int t = (int)(Math.random() * 10);
System.out.printf("%s: sleep for %d seconds. /n", thName, t);
Thread.sleep(t * 1000);
// 等待barrier
barrier.await();
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
e.printStackTrace();
return;
}
catch (BrokenBarrierException e)
{
e.printStackTrace();
return;
}
}
}
private static class BarrierAction implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
System.out.printf("%s: barrier action. /n", thName);
}
}
}
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1. 创建CyclicBarrier时通过构造函数指定线程的数量n。
2. 每个线程调用await方法等待其他线程。
3. 当有n个await方法被调用时,所有处于await的线程都将继续执行。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args)
{
// 初始化
barrier = new CyclicBarrier(10);
// 启动10个线程
System.out.println("main: starting 10 threads.");
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
try
{
// 随机等待5-14秒
int n = (int)(Math.random() * 10 + 5);
System.out.printf("%s: sleep for %d seconds. /n", thName, n);
Thread.sleep(n * 1000);
// 等待其他线程
System.out.printf("%s: waiting for other threads. /n", thName);
barrier.await();
// 结束
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
return;
}
catch (BrokenBarrierException e)
{
System.out.printf("%s: broken barrier. /n", thName);
return;
}
}
}
}
1. 在调用await方法时设置超时时间。
2. 当有一个await方法超时,所有处于await等待的线程将收到BrokenBarrierException,超时的线程自身将收到TimeoutException。
3. BrokenBarrier之后的所有await调用将直接抛出BrokenBarrierException。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class CyclicBarrierBrokenTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args)
{
// 初始化
barrier = new CyclicBarrier(10);
// 启动10个线程
System.out.println("main: starting 10 threads.");
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
try
{
// 随机等待5-14秒
int n = (int)(Math.random() * 10 + 5);
System.out.printf("%s: sleep for %d seconds. /n", thName, n);
Thread.sleep(n * 1000);
// 等待其他线程
System.out.printf("%s: waiting for other threads. /n", thName);
barrier.await(5, TimeUnit.SECONDS);
// 结束
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
return;
}
catch (BrokenBarrierException e)
{
System.out.printf("%s: broken barrier. /n", thName);
return;
}
catch (TimeoutException e)
{
System.out.printf("%s: timeout. /n", thName);
return;
}
}
}
}
当调用await线程达到CyclicBarrier的设置之后,CyclicBarrier将被重置,重新等待n个await调用。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierResetTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args) throws Exception
{
barrier = new CyclicBarrier(2);
Thread th1 = new Thread(new TestRunnable(), "thread 1");
Thread th2 = new Thread(new TestRunnable(), "thread 2");
th1.start();
th2.start();
Thread.sleep(60 * 1000);
th1.interrupt();
th2.interrupt();
th1.join();
th2.join();
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
System.out.printf("%s: start. /n", thName);
try
{
while (true)
{
// 随机产生1-100的整数。
int c = (int)(Math.random() * 100) + 1;
// 随机等待0-4秒。
int t = (int)(Math.random() * 5);
System.out.printf("%s: sleep for %d seconds./n", thName, t);
Thread.sleep(t * 1000);
// 等待另1线程。
barrier.await();
// 输出产生的整数。
System.out.printf("%s: %d /n", thName, c);
// 等待其他线程输出。
Thread.sleep(100);
}
}
catch (InterruptedException e)
{
System.out.printf("%s: interrupted. /n", thName);
}
catch (BrokenBarrierException e)
{
e.printStackTrace();
}
System.out.printf("%s: end. /n", thName);
}
}
}
1. 在创建CyclicBarrier的时候指定通过Barrier时需要执行的语句。
2. 通过构造方法的Runnable参数指定。
3. Runnable中的语句将由最后达到Barrier的线程执行。
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierActionTest
{
private static CyclicBarrier barrier = null;
public static void main(String[] args) throws Exception
{
barrier = new CyclicBarrier(10, new BarrierAction());
for (int i = 0; i < 10; i++)
{
Thread th = new Thread(new TestRunnable(), "thread " + i);
th.start();
}
}
private static class TestRunnable implements Runnable
{
@Override
public void run()
{
try
{
String thName = Thread.currentThread().getName();
// System.out.printf("%s: start. /n", thName);
// 随机等待0-9秒
int t = (int)(Math.random() * 10);
System.out.printf("%s: sleep for %d seconds. /n", thName, t);
Thread.sleep(t * 1000);
// 等待barrier
barrier.await();
System.out.printf("%s: end. /n", thName);
}
catch (InterruptedException e)
{
e.printStackTrace();
return;
}
catch (BrokenBarrierException e)
{
e.printStackTrace();
return;
}
}
}
private static class BarrierAction implements Runnable
{
@Override
public void run()
{
String thName = Thread.currentThread().getName();
System.out.printf("%s: barrier action. /n", thName);
}
}
}
相关文章推荐
- java线程同步的理解
- 实现Java线程同步的五种方法
- Java线程:线程的同步-同步块
- java线程同步(生产者消费者应用-模拟叫号系统)
- Java线程之线程同步
- java线程同步: synchronized详解(转)
- Java线程同步:synchronized锁住的是代码还是对象
- JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池
- Java synchronized同步线程机制(一)
- Java + 线程系列之synchronized同步锁(五)
- Java多线程-线程的同步与锁
- Java基础-23总结多线程,线程实现Runnable接口,线程名字获取和设置,线程控制,线程安全,同步线程
- java线程的同步中notify和wait方法之生产消费实例讲解
- java中的线程(二)——线程的同步和synchronized深入理解
- Java中线程的同步
- java线程同步机制
- Java多线程学习笔记——从Java JVM对多线程数据同步的一些理解
- Java多线程-线程的同步(同步代码块)
- java线程同步入门例子
- JAVA线程同步(一)