public class TestThread { private static class FirstThread extends Thread{ public void run() { { System.out.println("thread"); } } } private static class runThread implements Runnable{ @Override public void run() { System.out.println("run"); } } private static class CallThread implements Callable<String>{ @Override public String call() throws Exception { return "call"; } } public static void main(String[] args) throws InterruptedException, ExecutionException { new Thread(new FirstThread()).start(); new Thread(new runThread()).start();
CallThread callThread = new CallThread(); //FutureTask实现了Runnable接口的,所以这样封装之后就可以使用 FutureTask<String> futureTask = new FutureTask<>(callThread); new Thread(futureTask).start();
public class EndThread { private static class UseThread extends Thread { public UseThread(String name) { super(name); } @Override public void run() { String currentName = Thread.currentThread() .getName(); while (!isInterrupted()) { System.out.println(currentName + " is running!"); } System.out.println(currentName + " is stoped!"); } } public static void main(String[] args) throws InterruptedException { Thread thread = new UseThread("endThread"); thread.start(); Thread.sleep(10); thread.interrupt(); } }
运行结果:
endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is stoped!
Runnable:
public class EndRunnable {
private static class UseRunnable implements Runnable { @Override public void run() { String currentName = Thread.currentThread().getName(); while (!Thread.currentThread().isInterrupted()) { System.out.println(currentName + " is running!"); } System.out.println(currentName + " is stoped!"); } } public static void main(String[] args) throws InterruptedException { UseRunnable useRunnable = new UseRunnable(); Thread thread = new Thread(useRunnable,"endThread"); thread.start(); Thread.sleep(10); thread.interrupt(); } }
运行结果:
endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is running! endThread is stoped!
callable:
抛出InterruptedException异常的处理:
public class EndRunnable {
private static class UseRunnable implements Runnable { @Override public void run() { String currentName = Thread.currentThread().getName(); while (!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(currentName + "-"+Thread.currentThread().isInterrupted()); } System.out.println(currentName + " is stoped!--"+Thread.currentThread().isInterrupted()); } } public static void main(String[] args) throws InterruptedException { UseRunnable useRunnable = new UseRunnable(); Thread thread = new Thread(useRunnable,"endThread"); thread.start(); Thread.sleep(10); thread.interrupt(); } }