2014年5月25日
摘要: 1.主线程不能捕获到子线程的异常 package Thread.Exection; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExeceptionThread implements Runnable { @Override public void run() { throw new RuntimeException(); } 阅读全文
posted @ 2014-05-25 23:49 上校 阅读(13388) 评论(0) 推荐(0)
摘要: 将另外一个线程join到当前线程,则需要等到join进来的线程执行完才会继续执行当前线程。 package Thread.join; class Sleeper extends Thread { private int duration; public Sleeper(String name, int sleepTime) { super(name); duration = sleepTime; start(); } 阅读全文
posted @ 2014-05-25 15:01 上校 阅读(6737) 评论(0) 推荐(0)
摘要: 后台线程不执行finally package wzh.daemon; import java.util.concurrent.TimeUnit; class ADaemon implements Runnable { @Override public void run() { 阅读全文
posted @ 2014-05-25 13:42 上校 阅读(3207) 评论(0) 推荐(0)
摘要: 通过DaemonThreadFactory创建后台线程池 package wzh.daemon; import java.util.concurrent.ThreadFactory; public class DaemonThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setDaemon(true); return t; 阅读全文
posted @ 2014-05-25 13:26 上校 阅读(3202) 评论(0) 推荐(0)
摘要: 将线程设置成后台线程Daemons 主线程结果后,后台线程将自动结果。 package wzh.test; import java.util.concurrent.TimeUnit; class SimpleDaemons implements Runnable{ @Override public void run() { try { while (true) { 阅读全文
posted @ 2014-05-25 12:26 上校 阅读(1316) 评论(0) 推荐(0)
摘要: package wzh.test; import java.util.ArrayList; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; class TaskWithResult implements Callable{ private int id; public TaskWithResult(int id){ this.id=id; } @Override public String call() throws Exception { return "result of TaskWithResult "+id; } } 阅读全文
posted @ 2014-05-25 10:46 上校 阅读(1905) 评论(0) 推荐(0)
摘要: 原文:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html 在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。 对于直接继承Thread的类来说,代码大致框架是: 阅读全文
posted @ 2014-05-25 09:46 上校 阅读(488) 评论(0) 推荐(1)