您好,欢迎访问一九零五行业门户网

如何在Java中创建并运行线程?

一、创建并运行线程的五种方法第一种:继承thread类这种方式是最基础的一种方式,学过java的朋友都知道,不做赘述。需要注意的是:覆盖实现使用的是run方法,运行线程是start方法。
public class firstway extends thread { @override public void run() { system.out.println("第一种实现线程的方式:继承thread类"); } //模拟测试 public static void main(string[] args) { new firstway().start(); }}
第二种:实现runnable接口第二种实现方式仍然很基础,继承runnable接口,重写run方法实现线程运行逻辑。需要注意的:运行线程需要套一层new thread 。
public class secondway implements runnable{ @override public void run() { system.out.println("第二种实现线程的方式:实现runnable接口"); } //模拟测试 public static void main(string[] args) { new thread(new secondway()).start(); }}
第三种:实现callable接口第三种方式是实现callable接口,callable接口与runable接口都能实现线程。
public class thirdway implements callable<string> { @override public string call() throws exception { system.out.println("第三种实现线程的方式:实现callable接口"); return "callable接口带返回值,可以抛出异常"; } //模拟测试 public static void main(string[] args) throws executionexception, interruptedexception { futuretask<string> futuretask = new futuretask<>(new thirdway()); new thread(futuretask).start(); //阻塞方法,获取call方法返回值 system.out.println(futuretask.get()); //打印:callable接口带返回值,可以抛出异常 }}
区别如下:
callable接口实现线程方法是call, runable接口实现线程方法是run
callable有返回值, runable接口不能有返回值
callable接口方法call返回值可以设置泛型,如下例子中使用string数据类型
callable接口方法call方法可以抛出异常,runable接口run方法不可以抛出异常
callable接口方法通过new thread(futuretask).start()运行,futuretask的get方法可以获取callable接口方法call方法的返回值
如果callable接口方法call方法异常,在futuretask的get方法调用时也会抛出同样的异常
第四种:线程池 + execute从jdk5版本开始,java默认提供了线程池的支持,用线程池的方式运行线程可以避免线程的无限扩张导致应用宕机,同时也节省了线程频繁创建与销毁的资源与时间成本。
public class fourthway implements runnable{ @override public void run() { system.out.println(thread.currentthread().getname() + ":实现线程的方式runnable接口,但运行方式不一样,使用线程池"); } public static void main(string[] args) { //创建一个固定大小的线程池 executorservice threadpool = executors.newfixedthreadpool(5); for(int i = 0;i < 10;i++){ threadpool.execute(new fourthway()); } }}
线程池executorservice使用execute方法运行runnable接口run方法的线程实现,execute方法与run方法的共同特点是没有返回值。
pool-1-thread-5:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-2:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-4:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-4:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-4:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-1:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-4:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-3:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-2:实现线程的方式runnable接口,但运行方式不一样,使用线程池pool-1-thread-5:实现线程的方式runnable接口,但运行方式不一样,使用线程池
从上面的结果中可以看出,线程池中包含五个线程。线程运行完成之后并不销毁,而是还回到线程池,下一次执行时从线程池中获取线程资源再次运行。
第五种:线程池 + submit下面的例子线程池executorservice使用submit方法运行callable接口call方法的线程实现,submit方法与call方法的共同特点是存在返回值。
callable接口call方法的返回值可以由泛型定义
executorservice线程池submit方法的返回值是future
future的get方法可以获取call方法的返回值,同时如果call方法抛出异常,future的get方法也会抛出异常。
public class fifthway implements callable<string> { @override public string call() throws exception { return thread.currentthread().getname() + ":callable接口带返回值,可以抛出异常"; } //模拟测试 public static void main(string[] args) throws executionexception, interruptedexception { //保存多线程执行结果 list<string> retlist = new arraylist<>(); //创建一个固定大小的线程池 executorservice threadpool = executors.newfixedthreadpool(5); for(int i = 0;i < 10;i++){ future<string> future = threadpool.submit(new fifthway()); retlist.add(future.get()); } //java8 语法,打印retlist retlist.foreach(system.out::println); }}
上文代码中有一个小小的语法糖,retlist.foreach(system.out::println);是java8提供的方法引用
pool-1-thread-1:callable接口带返回值,可以抛出异常pool-1-thread-2:callable接口带返回值,可以抛出异常pool-1-thread-3:callable接口带返回值,可以抛出异常pool-1-thread-4:callable接口带返回值,可以抛出异常pool-1-thread-5:callable接口带返回值,可以抛出异常pool-1-thread-1:callable接口带返回值,可以抛出异常pool-1-thread-2:callable接口带返回值,可以抛出异常pool-1-thread-3:callable接口带返回值,可以抛出异常pool-1-thread-4:callable接口带返回值,可以抛出异常pool-1-thread-5:callable接口带返回值,可以抛出异常
以上就是如何在java中创建并运行线程?的详细内容。
其它类似信息

推荐信息