多线程
实现Callable接口 的 call() 方法 可以实现多线程,并且返回call() 方法的执行结果。
public class CallTest implements Callable<Object>{ public Object call() throws Exception { try { Thread.sleep(500L); System.out.println(Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("这是call 线程"); return 222; } }
Callable<Object> 也可以写成 Callable<String> 等类型, 返回 的也是 String 。
public class ThreadMain { public static void main(String[] args) { Callable<Object> callTest = new CallTest(); FutureTask<Object> fu = new FutureTask<Object>(callTest); new Thread(fu).start(); System.out.println("AA"); try { System.out.println(fu.get()); System.out.println("BB"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("这是主线程"); } }
执行结果:
AA
Thread-0
这是call 线程
222
BB
这是主线程
启动方式:
Callable<Object> callTest = new CallTest(); FutureTask<Object> fu = new FutureTask<Object>(callTest); new Thread(fu).start();
如果Callable 泛型是 Callable<String> , 则此处也必须是 FutureTask<String>。
fu.get()可以得到线程的返回结果。 如果执行 fu.get() , 则此处必须等待线程执行完。 才会执行后面的语句。
如果不需要执行 fu.get(), 则可以在线程执行前就 执行main 中后面的代码。比如如果把 try 注释掉。则返回:
AA
这是主线程
Thread-0
这是call 线程
当一个线程把另一个线程执行 interrupt() 中断时,如果另一个线程在运行状态,则 另一个线程只是 isInterrupted() 状态位改成 true ,并不会终止。
public class InterruptedDemo implements Runnable{ public static void main(String[] args) throws InterruptedException { Thread threadc = new Thread(new InterruptedDemo(), "InterruptedDemo thread") ; System.out.println("starting thread"); threadc.start(); System.out.println("interrupting thread"); threadc.interrupt(); System.out.println("线程是否中断:" + threadc.isInterrupted()); System.out.println("Stopping application"); } @Override public void run() { boolean stop = false; while(!stop) { System.out.println("My thread is running"); long time = System.currentTimeMillis(); System.out.println("线程是否中断22222:" + Thread.currentThread().isInterrupted()); } System.out.println("线程是否中断3333:" + Thread.currentThread().isInterrupted()); System.out.println("My thread exiting under request"); } }
输出:
starting thread
interrupting thread
线程是否中断:true
Stopping application
My thread is running
线程是否中断22222:true
My thread is running
线程是否中断22222:true
线程 threadc 会一直在 while循环中。
如果在 while 循环中 加上:
if (Thread.currentThread().isInterrupted()) {
break;
} 则会跳出循环。
当执行 threadc.interrupt(); 线程中断位置为真, isInterrupted() 返回true, 处于中断状态的线程 收到中断信号 Thread.sleep(),Object.wait(),Thread.join()等,会抛出InterruptedException,同时会把中断状态置回为false 。
当一个线程处于中断状态时(意思是它的中断状态位为true),如果再由wait、sleep以及jion三个方法引起的阻塞,那么JVM会将线程的中断标志重新设置为false,并抛出一个InterruptedException异常;
package demo.interrupt; public class InterruptedDemo implements Runnable{ public static void main(String[] args) throws InterruptedException { Thread threadc = new Thread(new InterruptedDemo(), "InterruptedDemo thread") ; System.out.println("starting thread"); threadc.start(); System.out.println("interrupting thread"); threadc.interrupt(); System.out.println("线程是否中断:" + threadc.isInterrupted()); System.out.println("Stopping application"); } @Override public void run() { boolean stop = false; while(!stop) { System.out.println("My thread is running"); long time = System.currentTimeMillis(); System.out.println("线程是否中断22222:" + Thread.currentThread().isInterrupted()); try { Thread.sleep(3L); System.out.println("111"); } catch (InterruptedException e) { System.out.println("222"); break; } } System.out.println("线程是否中断3333:" + Thread.currentThread().isInterrupted()); System.out.println("My thread exiting under request"); } }
输出:
starting thread
interrupting thread
线程是否中断:true
Stopping application
My thread is running
线程是否中断22222:true
222 // 中断标志置为 true 之后, Thread.sleep(3L); 会抛异常,执行 catch 中的语句。
线程是否中断3333:false
My thread exiting under request
public static boolean interrupted():返回线程的上次的中断状态,并清除中断状态
package demo.interrupt; public class InterruptedDemo implements Runnable{ public static void main(String[] args) throws InterruptedException { Thread threadc = new Thread(new InterruptedDemo(), "InterruptedDemo thread") ; System.out.println("starting thread"); threadc.start(); System.out.println("interrupting thread"); threadc.interrupt(); System.out.println("线程interrupted:" + threadc.interrupted()); System.out.println("线程是否中断:" + threadc.isInterrupted()); System.out.println("Stopping application"); } @Override public void run() { boolean stop = false; while(!stop) { System.out.println("My thread is running"); long time = System.currentTimeMillis(); System.out.println("线程是否中断22222:" + Thread.currentThread().isInterrupted()); System.out.println("线程22222 interrupted:" + Thread.interrupted()); try { Thread.sleep(3L); System.out.println("111"); } catch (InterruptedException e) { System.out.println("222"); break; } } System.out.println("线程是否中断3333:" + Thread.currentThread().isInterrupted()); System.out.println("My thread exiting under request"); } }
输出:
starting thread
interrupting thread
线程interrupted:false // interrupted() 方法是 static , 需要用类名调用
线程是否中断:true
My thread is running
Stopping application
线程是否中断22222:true
线程22222 interrupted:true // 中断状态的线程,执行 interrupted() 后,返回中断状态, 如果 中断状态是 true , 返回true,并清除中断状态
111
My thread is running
线程是否中断22222:false
线程22222 interrupted:false
111