一、继承Thread类
继承Thread类创建线程的步骤为:
(1)创建一个类继承Thread类,重写run()方法,将所要完成的任务代码写进run()方法中;
(2)创建Thread类的子类的对象;
(3)调用该对象的start()方法,该start()方法表示先开启线程,然后调用run()方法;
public class Thread1 { public static void main(String[] args) { Thread.currentThread().setName("主线程"); System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); //创建一个新线程 ThreadDemo1 thread1 = new ThreadDemo1(); //为线程设置名称 thread1.setName("线程一"); //开启线程 thread1.start(); } } class ThreadDemo1 extends Thread{ @Override public void run() { System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); } }
二、实现Runnable接口
实现Runnable接口创建线程的步骤为:
(1)创建一个类并实现Runnable接口
(2)重写run()方法,将所要完成的任务代码写进run()方法中
(3)创建实现Runnable接口的类的对象,将该对象当做Thread类的构造方法中的参数传进去
(4)使用Thread类的构造方法创建一个对象,并调用start()方法即可运行该线程
public class Thread2 { public static void main(String[] args) { Thread.currentThread().setName("主线程"); System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); //创建一个新线程 Thread thread2 = new Thread(new ThreadDemo2()); //为线程设置名称 thread2.setName("线程二"); //开启线程 thread2.start(); } } class ThreadDemo2 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); } }
三、实现Callable接口
实现Callable接口创建线程的步骤为:
(1)创建一个类并实现Callable接口
(2)重写call()方法,将所要完成的任务的代码写进call()方法中,需要注意的是call()方法有返回值,并且可以抛出异常
(3)如果想要获取运行该线程后的返回值,需要创建Future接口的实现类的对象,即FutureTask类的对象,调用该对象的get()方法可获取call()方法的返回值
(4)使用Thread类的有参构造器创建对象,将FutureTask类的对象当做参数传进去,然后调用start()方法开启并运行该线程。
public class Thread3 { public static void main(String[] args) throws Exception { Thread.currentThread().setName("主线程"); System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); //创建FutureTask的对象 FutureTask<String> task = new FutureTask<String>(new ThreadDemo3()); //创建Thread类的对象 Thread thread3 = new Thread(task); thread3.setName("线程三"); //开启线程 thread3.start(); //获取call()方法的返回值,即线程运行结束后的返回值 String result = task.get(); System.out.println(result); } } class ThreadDemo3 implements Callable<String> { @Override public String call() throws Exception { System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); return Thread.currentThread().getName()+":"+"返回的结果"; } }
四、使用线程池创建
使用线程池创建线程的步骤:
(1)使用Executors类中的newFixedThreadPool(int num)方法创建一个线程数量为num的线程池
(2)调用线程池中的execute()方法执行由实现Runnable接口创建的线程;调用submit()方法执行由实现Callable接口创建的线程
(3)调用线程池中的shutdown()方法关闭线程池
public class Thread4 { public static void main(String[] args) throws Exception { Thread.currentThread().setName("主线程"); System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); //通过线程池工厂创建线程数量为2的线程池 ExecutorService service = Executors.newFixedThreadPool(2); //执行线程,execute()适用于实现Runnable接口创建的线程 service.execute(new ThreadDemo4()); service.execute(new ThreadDemo6()); service.execute(new ThreadDemo7()); //submit()适用于实现Callable接口创建的线程 Future<String> task = service.submit(new ThreadDemo5()); //获取call()方法的返回值 String result = task.get(); System.out.println(result); //关闭线程池 service.shutdown(); } } //实现Runnable接口 class ThreadDemo4 implements Runnable{ @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); } } //实现Callable接口 class ThreadDemo5 implements Callable<String>{ @Override public String call() throws Exception { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); return Thread.currentThread().getName()+":"+"返回的结果"; } } //实现Runnable接口 class ThreadDemo6 implements Runnable{ @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); } } //实现Runnable接口 class ThreadDemo7 implements Runnable{ @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+":"+"输出的结果"); } }
原文:https://www.cnblogs.com/li666/p/11139968.html