下文讲述Java中创建线程的方法分享,如下所示:
实现 Runnable,一个类只需运行一个方法run
在继承类中我们需重写run()方法,需调用start()方法启动运行
例:
实现思路: 1.实现Runnable接口的形式创建 2.继承Thread类的创建 3.Callable和Future创建线程
采用Runnable接口的方式来创建线程
我们可以通过实现一个实现Runnable接口的类的方式创建线程,实现 Runnable,一个类只需运行一个方法run
public void run(); run方法中可调用其它方法,使用其它类,声明变量等操作 在类中,我们可以实例化一个线程对象, Thread(Runnable threadOb,String threadName); threadOb是一个实现Runnable接口的类的实例 threadName是新线程的名字 start()是运行新线程的方法例:
class ThreadTest implements Runnable { private Thread t; private String threadName; ThreadTest( String name) { threadName = name; System.out.println("创建 " + threadName ); } public void run() { System.out.println("运行 " + threadName ); try { for(int i = 6; i > 0; i--) { System.out.println("线程 " + threadName + ", " + i); // 让线程睡眠一会 Thread.sleep(30); } }catch (InterruptedException e) { System.out.println("线程 " + threadName + " 中断"); } System.out.println("线程 " + threadName + " 已存在"); } public void start () { System.out.println("开始 " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } public class TestMain { public static void main(String args[]) { ThreadTest R1 = new ThreadTest( "Thread-1"); R1.start(); ThreadTest R2 = new ThreadTest( "Thread-2"); R2.start(); } }
继承Thread来创建线程来创建线程
使用继承Thread的方式创建线程同Runnable接口类似,在继承类中我们需重写run()方法,需调用start()方法启动运行
例:
class ThreadDemo extends Thread { private Thread t; private String threadName; ThreadDemo( String name) { threadName = name; System.out.println("创建 " + threadName ); } public void run() { System.out.println("运行 " + threadName ); try { for(int i = 6; i > 0; i--) { System.out.println("线程: " + threadName + ", " + i); // 让线程睡眠一会 Thread.sleep(50); } }catch (InterruptedException e) { System.out.println("线程 " + threadName + " 中断"); } System.out.println("线程 " + threadName + " 存在"); } public void start () { System.out.println("开始 " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } public class TestThread { public static void main(String args[]) { ThreadDemo T1 = new ThreadDemo( "线程-1"); T1.start(); ThreadDemo T2 = new ThreadDemo( "线程-2"); T2.start(); } }
使用Callable及Future创建线程
- 创建Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值
- 创建Callable 实现类的实例,使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值
- 使用FutureTask 对象作为 Thread 对象的 target 创建并启动新线程
- 调用FutureTask 对象的 get() 方法来获得子线程执行结束后的返回值
public class CallableThreadTest implements Callable<Integer> { public static void main(String[] args) { CallableThreadTest ctt = new CallableThreadTest(); FutureTask<Integer> ft = new FutureTask<>(ctt); for(int i = 0;i < 80;i++) { System.out.println(Thread.currentThread().getName()+" 的循环变量i的值"+i); if(i==20) { new Thread(ft,"有返回值的线程").start(); } } try { System.out.println("子线程的返回值:"+ft.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } @Override public Integer call() throws Exception { int i = 0; for(;i<100;i++) { System.out.println(Thread.currentThread().getName()+" "+i); } return i; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。