public class LiftOff implements Runnable {
protected int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++;
public void run() {
while(countDown-- > 0 ) {
System.out.print("#"+id+"("+countDown+"). ");
Thread.yield();
}
}
}public class MainThread {
public static void main(String[] args) {
LiftOff lauch = new LiftOff();
lauch.run();
}
}.png)
public class BasicThreads {
public static void main(String[] args) {
Thread t = new Thread(new LiftOff());
t.start();
System.out.println("Waiting for LiftOff");
}
}.png)
public class MoreBasicThreads {
public static void main(String args[]) {
LiftOff liftOff = new LiftOff();
for(int i = 0; i < 5; i++) {
new Thread(liftOff).start();
}
}
}.png)
public class CachedThreadPool {
public static void main(String []args) {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0 ; i < 5; i++)
exec.execute( new LiftOff() );
exec.shutdown();
}
}public class FixedThreadPool{
public static void main(String []args) {
ExecutorService exec = Executors.FixedThreadPool();
for(int i = 0 ; i < 5; i++)
exec.execute( new LiftOff() );
exec.shutdown();
}
}public class SingleThreadExecutor {
public static void main(String []args) {
ExecutorService exec = Executors.newSingleThreadExecutor();
for(int i = 0 ; i < 5; i++)
exec.execute( new LiftOff() );
exec.shutdown();
}
}class TaskWithResult implements Callable<String> {
private int id;
public TaskWithResult(int id) {
this.id = id;
}
@Override
public String call() throws Exception {
return "result of TaskWithResult " + id;
}
}
public class CallableDemo {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> results = new ArrayList<Future<String>>();
for(int i = 0; i < 10;i++) {
results.add( exec.submit(new TaskWithResult(i)));
}
for(Future<String> fs : results)
try {
System.out.println(fs.get());
} catch (InterruptedException e) {
System.out.println(e);
} catch (ExecutionException e) {
System.out.println(e);
}
exec.shutdown();
}
}public class SimplePriorities implements Runnable{
private int countDown = 5;
private double d;
private int priority;
public SimplePriorities(int priority) {
this.priority = priority;
}
public String toString( ) {
return Thread.currentThread() + " : " + countDown;
}
public void run( ) {
Thread.currentThread().setPriority( priority );
while(true) {
for(int i = 1 ; i < 100000 ; i++) {
d += (Math.PI + Math.E) / (double)i;
if(i % 1000 == 0)
Thread.yield();
}
System.out.println(this);
if( --countDown == 0) return;
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0; i < 5; i++)
exec.execute( new SimplePriorities(Thread.MIN_PRIORITY));
exec.execute( new SimplePriorities(Thread.MAX_PRIORITY));
exec.shutdown();
}
}public class SimpleDaemons implements Runnable {
public void run() {
try{
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(Thread.currentThread()+" "+this);
}
} catch (InterruptedException e) {
System.out.println("sleep() interrupted");
}
}
public static void main(String []args) throws Exception {
for(int i = 0; i < 10; i++) {
Thread daemon = new Thread(new SimpleDaemons());
daemon.setDaemon(true);
daemon.start();
}
System.out.println("All daemons started");
TimeUnit.MILLISECONDS.sleep( 175 );
}
} public class DaemonThreadFactory implements ThreadFactory{
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
}
public class DaemonFromFactory implements Runnable {
@Override
public void run() {
try{
while(true) {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println( Thread.currentThread() + " " +this);
}
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
public static void main(String args[]) throws Exception {
ExecutorService exec = Executors.newCachedThreadPool();
for(int i = 0; i < 10; i++)
exec.execute(new DaemonFromFactory());
System.out.println("All daemons started");
TimeUnit.MILLISECONDS.sleep(500);
}
}class ExceptionThread2 implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run() by " + t);
System.out.println("eh = " + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
}
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught " + e);
}
}
class HandlerThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
System.out.println(this + " creating new Thread");
Thread t = new Thread(r);
System.out.println("created " + t);
t.setUncaughtExceptionHandler( new MyUncaughtExceptionHandler());
System.out.println("eh = " + t.getUncaughtExceptionHandler());
return t;
}
}
public class CaptureUncaughtException {
public static void main(String []args) {
ExecutorService exec = Executors.newCachedThreadPool(new HandlerThreadFactory());
exec.execute(new ExceptionThread2());
}
}public class SettingDefaultHandler {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(
new MyUncaughtExceptionHandler( ) );
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new ExceptionThread());
}
} 原文地址:http://blog.csdn.net/troy__/article/details/40477423