Java多线程——之一创建线程的四种方法
2018-10-19 06:29:44来源:博客园 阅读 ()
1.实现Runnable接口,重载run(),无返回值
package thread; public class ThreadRunnable implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } } } package thread; public class ThreadMain { public static void main(String[] args) throws Exception { ThreadRunnable threadRunnable1 = new ThreadRunnable(); ThreadRunnable threadRunnable2 = new ThreadRunnable(); ThreadRunnable threadRunnable3 = new ThreadRunnable(); ThreadRunnable threadRunnable4 = new ThreadRunnable(); Thread thread1 = new Thread(threadRunnable1); Thread thread2 = new Thread(threadRunnable2); Thread thread3 = new Thread(threadRunnable3); Thread thread4 = new Thread(threadRunnable4); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }
2.继承Thread类,复写run()
使用时通过调用Thread的start()(该方法是native),再调用创建线程的run(),不同线程的run方法里面的代码交替执行。
不足:由于java为单继承,若使用线程类已经有个父类,则不能使用该方式创建线程。
public class ThreadEx extends Thread { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread() + ":" + i); } } } public class ThreadMain { public static void main(String[] args) { ThreadEx threadEx = new ThreadEx(); threadEx.start(); } }
3.实现Callable接口,通过FutureTask/Future来创建有返回值的Thread线程,通过Executor执行
补充:与实现Runnable接口类似,都是实现接口,不同的是该方式有返回值,可以获得异步执行的结果。
延伸:FutureTask是类,Future是接口。
package thread; import java.util.concurrent.*; public class ThreadCallable { public static void main(String[] args) throws Exception { FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() { public Integer call() throws Exception { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } return 1; } }); Executor executor = Executors.newFixedThreadPool(1); ((ExecutorService) executor).submit(futureTask); //获得线程执行状态 System.out.println(Thread.currentThread().getName() + ":" + futureTask.get()); } }
4.使用Executors创建ExecutorService,入参Callable或Future
补充:适用于线程池和并发
package thread; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import static java.lang.Thread.sleep; public class ThreadExecutors { private final String threadName; public ThreadExecutors(String threadName) { this.threadName = threadName; } private ThreadFactory createThread() { ThreadFactory tf = new ThreadFactory() { public Thread newThread(Runnable r) { Thread thread = new Thread(); thread.setName(threadName); thread.setDaemon(true); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return thread; } }; return tf; } public Object runCallable(Callable callable) { return Executors.newSingleThreadExecutor(createThread()).submit(callable); } public Object runFunture(Runnable runnable) { return Executors.newSingleThreadExecutor(createThread()).submit(runnable); } } package thread; import java.util.concurrent.*; public class ThreadMain { public static void main(String[] args) throws Exception { ThreadExecutors threadExecutors = new ThreadExecutors("callableThread"); threadExecutors.runCallable(new Callable() { public String call() throws Exception { return "success"; } }); threadExecutors.runFunture(new Runnable() { public void run() { System.out.println("execute runnable thread."); } }); } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash