1、继承Thread类。并重写run()方法,该方法无参数,无返回值;
2、创建子类实例,并实例化对象;
3、通过start()方法启动,注意:不是通过run()方法启动。
public class ThreadDemo extends Thread{ public void run(){ System.out.println("hello ,我是通过继承Thread创建线程的。"); } } public class ThreadAction { public static void main(String[] args) { ThreadDemo demo = new ThreadDemo(); demo.start(); } }1、实现Runnable接口,并重新run()方法;
2、创建Runnable实现类的实例,并用这个实例作为Thread的target创建线程对象;
3、通过start()启动线程。
public class RunnableDemo implements Runnable{ /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { System.out.println("hello ,我是通过实现Runnable创建线程的。"); } } public static void main(String[] args) { RunnableDemo demo = new RunnableDemo(); Thread thread = new Thread(demo); thread.start(); }产生背景:这种创建线程的方式在jdk1.5后出现,目的是弥补上面两种创建线程方式,其中的run()方法不能有返回值,不能抛出异常等问题。
1、创建实现Callable的实现类,并实现call()方法;
2、创建该实现类的实例(从java8开始可以直接使用Lambda表达式创建Callable对象);
3、使用FutureTask类来包装Callable对象,该FutureTask对象封装了Callable对象的call()方法的返回值;
4、使用FutureTask对象作为Thread对象的target创建并启动线程(因为FutureTask实现了Runnable接口);
5、调用FutureTask对象的get()方法来获得子线程执行结束后的返回值。
public class CallableDemo implements Callable<String> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ @Override public String call() throws Exception { return "hello ,我是通过实现Callable接口的方式创建线程。"; } } public static void main(String[] args) { CallableDemo demo = new CallableDemo(); // 使用FutureTask类来包装Callable对象 FutureTask<String> futureTask = new FutureTask<>(demo); Thread thread = new Thread(futureTask); thread.start(); try { System.out.println(futureTask.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } }后续深入探讨线程池
转载于:https://www.cnblogs.com/yanfei1819/p/8950371.html