java创建线程方式
程序员文章站
2022-06-05 17:34:29
1.继承Thread类 public class ThreadCreator extends Thread{ public static void main(String[] args) { //第一种方式: ThreadCreator creator = new ThreadCreator(); ......
1.继承thread类
public class threadcreator extends thread{ public static void main(string[] args) {
//第一种方式: threadcreator creator = new threadcreator(); thread thread = new thread(creator,"线程1");
thread.start();
//第二种方式:
thread thread = new threadcreator();
thread.start();
//第三种方式:
new threadcreator().start();
} @override public void run() { system.out.println(thread.currentthread().getname() + "run"); } }
2.实现runnable接口
public class threadcreator implements runnable{ public static void main(string[] args) { threadcreator creator = new threadcreator(); thread thread = new thread(creator,"线程1"); thread.start(); } @override public void run() { system.out.println(thread.currentthread().getname() + "run"); } }
3.实现callable接口
public class threadcreator implements callable<integer> { public static void main(string[] args) throws executionexception, interruptedexception { threadcreator creator = new threadcreator(); futuretask futuretask = new futuretask(creator); thread thread = new thread(futuretask,"线程"); thread.start(); system.out.println(futuretask.get()); } @override public integer call() { return 1024; } }
4.线程池executorservice
public class threadcreator{ static executorservice service = executors.newfixedthreadpool(5); public static void main(string[] args) throws executionexception, interruptedexception { //execute无返回值 service.execute(new threadtask(1,"1")); //submit有返回值 future<integer> result = service.submit(new threadtaskcall()); system.out.println(result.get());
service.shutdownnow(); } static class threadtask implements runnable{ private int param1; private string param2; public threadtask(int param3,string param4){ this.param1 = param3; this.param2 = param4; } @override public void run() { system.out.println(param1+param2); } } static class threadtaskcall implements callable<integer>{ @override public integer call() throws exception { return 1024; } } }
线程池中submit和execute的区别:
① 可接受的任务类型不一样:execute只能接受runnable任务,submit还可以接受callable任务。
② 返回值:execute无返回值,任务一旦提交,无法在当前线程中监控执行结果。submit有一个future类型的返回值,用来接收返回值或响应异常。通过get()方法获取。
submit底层还是调用的execute,只是在此基础上用future封装了一层,并将执行过程中产生的异常全部封装在一个变量中:
public void run() { if (state != new || !unsafe.compareandswapobject(this, runneroffset, null, thread.currentthread())) return; try { callable<v> c = callable; if (c != null && state == new) { v result; boolean ran; try { result = c.call(); ran = true; } catch (throwable ex) { result = null; ran = false; setexception(ex); } if (ran) set(result); } } finally { runner = null; int s = state; if (s >= interrupting) handlepossiblecancellationinterrupt(s); } } protected void setexception(throwable t) { if (unsafe.compareandswapint(this, stateoffset, new, completing)) { outcome = t; unsafe.putorderedint(this, stateoffset, exceptional); // final state finishcompletion(); } }
另外,spring中的schedule注解借鉴使用了submit的处理方式。
5.匿名内部类
public class threadcreator { public static void main(string[] args) { //继承thread类 new thread() { @override public void run() { system.out.println("extends thread class!"); } }.start(); //实现runnable接口 new thread(new runnable() { @override public void run() { system.out.println("implement runnable!"); } }).start(); //实现callable接口 new thread(new futuretask<integer>(new callable() { @override public integer call() throws exception { return 1024; } })).start(); //lambda表达式 new thread(() -> system.out.println("execute single code")).start(); new thread(() -> { system.out.println("execute multiple code"); }).start(); } }
lambda线程池:
public class threadcreator { static executorservice service = executors.newfixedthreadpool(5); static list list = new arraylist(); public static void main(string[] args) { service.execute(() -> execute()); //无返回值 future future = service.submit(() -> execute()); //有返回值 list.add(future); } public static void execute() { //do something } }
下一篇: 【WPF学习】第一章 XAML介绍