There are three ways for java to start a new thread:
Class Thread direct new
Interface Runnable
Interface callable
The difference between Runnable and Callable:
Callable can return a value.
package com.jwz.test; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class testThread { private static class UserRun implements Runnable { @Override public void run(){ System.out.println("I am runable "); } } private static class UserCall implements Callable{ @Override public String call(){ System.out.println("I am callable"); return "CallResult"; } } public static void main(String[] args) throws ExecutionException, InterruptedException { UserRun run = new UserRun(); new Thread(run).start(); UserCall call=new UserCall(); //Constructing Callable Object with futureTask FutureTask<String> future=new FutureTask<String>(call); new Thread(future).start(); System.out.println(future.get()); } }
The end of the thread:
Early versions used stop(),resume(),suspend(), where stop() could not release resources and suspend() was prone to deadlock. So it is not recommended to use it.
Now use the interrput(), isInterrupted(), and static methods interrputed()
The interrput method is used to interrupt a thread (java threads are collaborative), not to force a thread to close, but to greet the thread and set the interrupt thread's flag bit to true. Whether the focus is decided by the thread itself.
isInterrupted determines whether the current thread is in an interrupt state
The static method interrupted determines whether the current thread is in an interrupt state and sets the thread interrupt flag bit to false.
package com.jwz.thread; public class EndThread { private static class UseThread extends Thread{ public UseThread(String name){ super(name); } @Override public void run(){ String threadName=Thread.currentThread().getName(); while (!isInterrupted()){ System.out.println(threadName+" is run"); } System.out.println(threadName+" is finished and isInterrupt flag is "+isInterrupted()); } public static void main(String[] args) throws InterruptedException { Thread useThread=new UseThread("EndThread"); useThread.start(); sleep(20); useThread.interrupt(); } } }
Threads do not interrupt without judging isInterrupted().
If the run() method has Thread.slee() method to grab the exception with try catch, the interrupt() method needs to be called in catch to break the thread.
public class EndThreadRun { private static class useRun implements Runnable{ @Override public void run(){ String threadName=Thread.currentThread().getName(); while(!Thread.currentThread().isInterrupted()){ try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(threadName+" is strop and interrupt is " +Thread.currentThread().isInterrupted()); e.printStackTrace(); Thread.currentThread().interrupt(); System.out.println(threadName); } System.out.println(threadName+" is strop and interrupt is " +Thread.currentThread().isInterrupted()); } } } public static void main(String[] args) throws InterruptedException { useRun useRun=new useRun(); Thread endThread=new Thread(useRun,"endThread"); endThread.start(); Thread.sleep(20); endThread.interrupt(); } }