Java多线程——之一创建线程的四种方法
程序员文章站
2022-07-11 09:57:56
1.实现Runnable接口,重载run(),无返回值 2.继承Thread类,复写run() 使用时通过调用Thread的start()(该方法是native),再调用创建线程的run(),不同线程的run方法里面的代码交替执行。 不足:由于java为单继承,若使用线程类已经有个父类,则不能使用该 ......
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.");
}
});
}
}
5 runnable接口和callable接口区别
1)两个接口需要实现的方法名不一样,runnable需要实现的方法为run(),callable需要实现的方法为call()。
2)实现的方法返回值不一样,runnable任务执行后无返回值,callable任务执行后可以得到异步计算的结果。
3)抛出异常不一样,runnable不可以抛出异常,callable可以抛出异常。