Java线程的四种方法

Java线程的四种方法:

1:继承Thread()类,重写run方法;(extends)

2:实现Runable接口,重写run方法;接口的实现类(MyThread)作为参数传给Thread的构造方法;(implements

3:通过Callable和FutureTask创建线程;(call方法

4:通过线程池创建线程;(Executors)

如果是run方法,如1/2:会先执行完main里面的函数在执行线程;

如果是call方法,如3/4:会按照顺序来输出;ExecutorService、Callable都是属于Executor框架。所以3和4是是一类

 

1:继承Thread:

public class Mythread extends Thread{

public Mythread() {

//子类构造方法:可缺省;

}

public void run() {

System.out.println("Mythread run: "+Thread.currentThread().getName());

}

public static void main(String []args) {

Mythread mythread=new Mythread();

mythread.setName("Mythread*");///如果没有此步就会输出Mythread run: Thread-0

mythread.start();//2步

System.out.println("Mythread main: "+Thread.currentThread().getName());//1步

}

}

输出:

Mythread main: main

Mythread run: Mythread*

 

2实现Runable接口

public class Main{

public static void main(String []args) {

System.out.println("Mythread2 main: "+Thread.currentThread().getName());//1步

Thread t1=new Thread(new Mythread2());

t1.start();//3步

    System.out.println("Mythread2 main: "+Thread.currentThread().getName());//2步

}

}

class Mythread2 implements Runnable{

@Override

public void run() {

System.out.println("Mythread2 run:"+Thread.currentThread().getName());

}

}

输出:

Mythread2 main: main

Mythread2 main: main

Mythread2 run:Thread-0

 

3使用Callable和FutureTask方法

具体步骤如下:

1)创建Callable接口的实现类 implcall,并且重写实现call方法(implements)

2)创建Callable实现类implcall的实例,使用FutureTask方法包装Callable对象的call的返回值

Callable<Object> oneCallable = new implcall<Object>();

FutureTask<Object> oneTask = new FutureTask<Object>(oneCallable);

3)使用FutureTask对象作为Thread的构造函数的参数Thread t = new Thread(oneTask);

4)创建并启动线程

5)调用FutureTask对象的get()来获取子线程执行结束的返回值;

 

public class Main{

public static void main(String []args) throws InterruptedException, ExecutionException {

Callable<String> callable =new implcall<String>();

FutureTask<String> task=new FutureTask<String>(callable);///2)

 

Thread thread=new Thread(task);///3)

System.out.println("thread main:"+Thread.currentThread().getName());///输出1

thread.start();///4)

System.out.println(task.get());///输出3:implcall***

System.out.println("thread main2:"+Thread.currentThread().getName());///输出4

}

}

class implcall<String> implements Callable<String>{///1)

@Override

public String call() throws Exception {

System.out.println("implcall call:"+Thread.currentThread().getName());///输出2

return (String) "implcall***";

}

}

输出:

thread main1:main

implcall call:Thread-0

Implcall***

thread main2:main

 

4使用线程池创建线程

步骤如下:

1)ExecutorService executorService = Executors.newFixedThreadPool(5);

2)impRunnable imt=new impRunnable();///创建 impRunnable 实例(其是runnable的实现类)

3)executorService.execute(imt);///(执行线程)

4)executorService.shutdown();///关闭线程

public class Main{

static int pollnum=5;

public static void main(String []args) {

ExecutorService eService=Executors.newFixedThreadPool(pollnum);

for(int i=0;i<pollnum;i++) {

impRunnable imt=new impRunnable();

eService.execute(imt);

//Thread.sleep(1000);///加上此行就会按顺序执行:输出12345,而不是乱序

}

eService.shutdown();

} 

}

class impRunnable implements Runnable{

@Override

public void run() {

 System.out.println("impRunnable run:" + Thread.currentThread().getName() );  

}

}

输出:

impRunnable run:pool-1-thread-1

impRunnable run:pool-1-thread-4

impRunnable run:pool-1-thread-3

impRunnable run:pool-1-thread-2

impRunnable run:pool-1-thread-5

参考:https://blog.csdn.net/u011480603/article/details/75332435

 

 

猜你喜欢

转载自blog.csdn.net/qq_39667655/article/details/82762729