java中多线实现共有四种方式:
* 继承thread
* 实现runnable接口
* 通过callable和FutureTask创建线程
* 通过线程池
--
1. 继承thread类,重写run方法,该实现方式没有返回值。
```
public class myThread extends Thread {
//private int i = 0;
public myThread() {
System.out.println("myThread init");
}
public void run() {
System.out.println(Thread.currentThread()+" is running ");
}
public static void main(String[] args) {
myThread th= new myThread();
th.start();
myThread th1= new myThread();
th1.start();
System.out.println(Thread.currentThread());
}
}
//myThread init
//myThread init
//Thread[Thread-0,5,main] is running
//Thread[main,5,main]
//Thread[Thread-1,5,main] is running
```
----
2. 如果自己的类继承了其他的类时,无法继承thread。此时可以通过implement runnable协议实现多线程。runnable 协议相比与thread很容易实现并发的多个线程共享资源。
```
public class runnableTest implements Runnable {
public void run() {
System.out.println("runnable is running " + Thread.currentThread());
}
public static void main(String[] args) {
Thread thread = new Thread(new runnableTest());
thread.start();
System.out.println(Thread.currentThread());
//runnableTest runnableTest = new runnableTest();
//runnableTest.run();
//此时runnable的任务分配给了主线程
}
}
//Thread[main,5,main]
//runnable is running Thread[Thread-0,5,main]
```
---
3. runnable接口是没有返回值的,而callable接口却有返回值,callable是一种具有类型参数的泛型,它的类型参数是call()方法的返回值。
```
class callableTask implements Callable<String> {
private int id;
public callableTask(int id) {
// TODO Auto-generated constructor stub
this.id = id;
}
@Override
public String call() throws Exception {
return "result of callableTask" + id;
}
}
public class CallableTest {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Future<String>> res = new ArrayList<Future<String>>() ;
for (int i = 0; i < 10; i++) {
res.add(exec.submit(new callableTask(i)));
}
for (Future<String> future : res) {
try {
System.out.println(future.get());
} catch (Exception e) {
// TODO: handle exception
System.out.println();
}finally {
exec.shutdown();
}
}
}
}
//result of callableTask0
//result of callableTask1
//result of callableTask2
//result of callableTask3
//result of callableTask4
//result of callableTask5
//result of callableTask6
//result of callableTask7
//result of callableTask8
//result of callableTask9
```
4. 通过线程池来实现多线程。CachedThreadPool、FixThreadPool、SingleThreadPool。
```
public class executorTest {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
ExecutorService executorService = Executors.newFixedThreadPool(5);
for(int i = 0; i<10; i++)
{
RunnableThread thread = new RunnableThread();
executorService.execute(thread);
}
//关闭线程池
executorService.shutdown();
}
}
class RunnableThread implements Runnable
{
@Override
public void run()
{
System.out.println("通过线程池方式创建的线程:" + Thread.currentThread().getName() + " ");
}
}
//通过线程池方式创建的线程:pool-1-thread-3
//通过线程池方式创建的线程:pool-1-thread-2
//通过线程池方式创建的线程:pool-1-thread-3
//通过线程池方式创建的线程:pool-1-thread-5
//通过线程池方式创建的线程:pool-1-thread-4
//通过线程池方式创建的线程:pool-1-thread-1
//通过线程池方式创建的线程:pool-1-thread-4
//通过线程池方式创建的线程:pool-1-thread-5
//通过线程池方式创建的线程:pool-1-thread-3
//通过线程池方式创建的线程:pool-1-thread-2
```