package concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
// only two threads
ExecutorService exec = Executors.newFixedThreadPool(2);
for(int index = 0; index < 100; index++) {
public void run() {
long time
= (long) (Math.
random() * 1000);
System.
out.
println(“Sleeping ”
+ time
+ “ms”
);
try {
}
}
};
exec.execute(run);
}
// must shutdown
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
public class TestScheduledThread {
public static void main
(String[] args
) {
final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(2);
int count = 0;
public void run() {
System.
out.
println(new Date() + ” beep ”
+ (++count
));
}
};
// 1秒钟后运行,并每隔2秒运行一次
final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(
beeper, 1, 2, SECONDS);
// 2秒钟后运行,并每次在上次任务运行完后等待5秒后重新运行
final ScheduledFuture beeperHandle2 = scheduler
.scheduleWithFixedDelay(beeper, 2, 5, SECONDS);
// 30秒后结束关闭任务,并且关闭Scheduler
public void run() {
beeperHandle.cancel(true);
beeperHandle2.cancel(true);
scheduler.shutdown();
}
}, 30, SECONDS);
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCyclicBarrier {
// 徒步需要的时间: Shenzhen, Guangzhou, Shaoguan, Changsha, Wuhan
private static int[] timeWalk = { 5, 8, 15, 15, 10 };
// 自驾游
private static int[] timeSelf = { 1, 3, 4, 4, 5 };
// 旅游大巴
private static int[] timeBus = { 2, 4, 6, 6, 7 };
return sdf.
format(new Date()) + “
: “
;
}
private int[] times;
private CyclicBarrier barrier;
public Tour
(CyclicBarrier barrier,
String tourName,
int[] times
) {
this.times = times;
this.tourName = tourName;
this.barrier = barrier;
}
public void run() {
try {
Thread.
sleep(times
[0] * 1000);
System.
out.
println(now
() + tourName
+ ” Reached Shenzhen”
);
barrier.await();
Thread.
sleep(times
[1] * 1000);
System.
out.
println(now
() + tourName
+ ” Reached Guangzhou”
);
barrier.await();
Thread.
sleep(times
[2] * 1000);
System.
out.
println(now
() + tourName
+ ” Reached Shaoguan”
);
barrier.await();
Thread.
sleep(times
[3] * 1000);
System.
out.
println(now
() + tourName
+ ” Reached Changsha”
);
barrier.await();
Thread.
sleep(times
[4] * 1000);
System.
out.
println(now
() + tourName
+ ” Reached Wuhan”
);
barrier.await();
} catch (BrokenBarrierException e) {
}
}
}
public static void main
(String[] args
) {
// 三个旅行团
CyclicBarrier barrier = new CyclicBarrier(3);
ExecutorService exec = Executors.newFixedThreadPool(3);
exec.submit(new Tour(barrier, “WalkTour”, timeWalk));
exec.submit(new Tour(barrier, “SelfTour”, timeSelf));
exec.submit(new Tour(barrier, “BusTour”, timeBus));
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
public class TestBlockingQueue {
static long randomTime() {
return (long) (Math.
random() * 1000);
}
public static void main
(String[] args
) {
// 能容纳100个文件
final BlockingQueue queue = new LinkedBlockingQueue(100);
// 线程池
final ExecutorService exec = Executors.newFixedThreadPool(5);
final File root
= new File(“F
:\\JavaLib”
);
// 完成标志
// 读个数
final AtomicInteger rc = new AtomicInteger();
// 写个数
final AtomicInteger wc = new AtomicInteger();
// 读线程
public void run() {
scanFile(root);
scanFile(exitFile);
}
public void scanFile
(File file
) {
if (file.isDirectory()) {
public boolean accept
(File pathname
) {
return pathname.isDirectory()
|| pathname.getPath().endsWith(“.java”);
}
});
scanFile(one);
} else {
try {
int index = rc.incrementAndGet();
System.
out.
println(“Read0
: ”
+ index
+ ” “
+ file.getPath());
queue.put(file);
}
}
}
};
exec.submit(read);
// 四个写线程
for (int index = 0; index < 4; index++) {
// write thread
final int NO = index;
String threadName
= “Write”
+ NO
;
public void run() {
while (true) {
try {
int index = wc.incrementAndGet();
File file
= queue.
take();
// 队列已经无对象
if (file == exitFile) {
// 再次添加”标志”,以让其他线程正常退出
queue.put(exitFile);
break;
}
System.
out.
println(threadName
+ “
: ”
+ index
+ ” “
+ file.getPath());
}
}
}
};
exec.submit(write);
}
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestCountDownLatch {
// 开始的倒数锁
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(10);
// 十名选手
final ExecutorService exec = Executors.newFixedThreadPool(10);
for(int index = 0; index < 10; index++) {
final int NO = index + 1;
public void run() {
try {
begin.await();
System.
out.
println(“No.”
+ NO
+ ” arrived”
);
} finally {
end.countDown();
}
}
};
exec.submit(run);
}
System.
out.
println(“Game Start”
);
begin.countDown();
end.await();
System.
out.
println(“Game Over”
);
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestFutureTask {
ExecutionException {
final ExecutorService exec = Executors.newFixedThreadPool(5);
Callable call = new Callable() {
return “Other less important but longtime things.”;
}
};
Future task = exec.submit(call);
// 重要的事情
System.
out.
println(“Let’s
do important things.”
);
// 其他不重要的事情
// 关闭线程池
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestCompletionService {
ExecutionException {
ExecutorService exec = Executors.newFixedThreadPool(10);
CompletionService serv =
new ExecutorCompletionService(exec);
for (int index = 0; index < 5; index++) {
final int NO = index;
Callable downImg = new Callable() {
return “Downloaded
Image ”
+ NO
;
}
};
serv.submit(downImg);
}
System.
out.
println(“Show web content”
);
for (int index = 0; index < 5; index++) {
Future task = serv.take();
}
// 关闭线程池
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net
package concurrent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class TestSemaphore {
public static void main
(String[] args
) {
// 线程池
ExecutorService exec = Executors.newCachedThreadPool();
// 只能5个线程同时访问
final Semaphore semp = new Semaphore(5);
// 模拟20个客户端访问
for (int index = 0; index < 20; index++) {
final int NO = index;
public void run() {
try {
// 获取许可
semp.acquire();
System.
out.
println(“Accessing
: ”
+ NO
);
// 访问完后,释放
semp.release();
}
}
};
exec.execute(run);
}
// 退出线程池
exec.shutdown();
}
}//源代码片段来自云代码http://yuncode.net