java创建线程的几种方式
生活随笔
收集整理的這篇文章主要介紹了
java创建线程的几种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.繼承Thread類
/*** @author Ash* @date: 2016年8月6日 下午10:56:45 * @func: 通過繼承Thread類來實現多線程* @email 408657544@qq.com* @Copyright: 2016 Ash. All rights reserved.*/ public class ExtendsThread extends Thread{public static void main(String[] args) {new ExtendsThread().start();}public void run() {System.out.println("hello");} }2.實現Runnable接口
package com.test.thread;public class ImplementsRunable {public static void main(String[] args) {new Thread(new Task()).start();} } class Task implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubSystem.out.println("hello");} }3.使用線程池
public class CallableFutureSample {public static void main(String[] args) throws InterruptedException, ExecutionException {ExecutorService pool = Executors.newCachedThreadPool();for (int i = 0; i < 3; i++) {System.out.println(pool.submit(new Task3(i+"")).get());}pool.shutdown();} }class Task3 implements Callable<String> {private String taskname;public Task3(String taskname) {this.taskname=taskname;}@Overridepublic String call() throws Exception {return taskname+" started";} }?
轉載于:https://www.cnblogs.com/heben/p/5745264.html
總結
以上是生活随笔為你收集整理的java创建线程的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端基础之设计一个个人工作室介绍界面
- 下一篇: SSIS同步多个数据库