首页 技术 正文
技术 2022年11月15日
0 收藏 762 点赞 2,215 浏览 3026 个字
  • 概述
    Java线程是一个在实战开发中经常使用的基础功能,而在Java中线程相关的类在java.lang和java.util.concurrent里

  • Thread

package thread.base;/** * User: likang * Date: 16/8/14 * Time: 下午4:27 */public class TestThread extends Thread {    private Integer index;    private String name;    public TestThread(String name, Integer index) {        this.name = name;        this.index = index;    }    public void run() {        try {            Thread.sleep(1000l);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(name + "Thread index:" + index);    }}
  • Runnable
package thread.base;/** * User: likang * Date: 16/8/14 * Time: 下午4:32 */public class TestRunnable implements Runnable {    private Integer index;    private String name;    public TestRunnable(String name, Integer index) {        this.name = name;        this.index = index;    }    public void run() {        try {            Thread.sleep(1000l);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println(name + "Runnable index:" + index);    }}
  • Callable
package thread.base;import java.util.concurrent.Callable;/** * User: likang * Date: 16/8/14 * Time: 下午4:35 */public class TestCallable implements Callable<String> {    private Integer index;    private String name;    public TestCallable(String name, Integer index) {        this.name = name;        this.index = index;    }    @Override    public String call() throws Exception {        try {            Thread.sleep(1000l);        } catch (InterruptedException e) {            e.printStackTrace();        }        return name + "Callable index:" + index;    }}
  • 实例测试
import thread.base.TestCallable;import thread.base.TestRunnable;import thread.base.TestThread;import java.util.concurrent.*;/** * User: likang * Date: 16/8/14 * Time: 下午4:26 */public class ThreadTest {    public static void main(String[] args) throws ExecutionException, InterruptedException {        System.out.println("测试第一部分 Thread");        for (Integer i = 0; i < 5; i++) {            TestThread testThread = new TestThread("第一部分 ", i);            testThread.start();        }        System.out.println("测试第二部分 Runnable+Thread");        for (Integer i = 0; i < 5; i++) {            TestRunnable testRunnable = new TestRunnable("第二部分 ", i);            new Thread(testRunnable).start();        }        System.out.println("测试第三部分 Callable+Thread");        for (Integer i = 0; i < 5; i++) {            TestCallable testCallable = new TestCallable("第三部分 ", i);            FutureTask<String> futureTask = new FutureTask<String>(testCallable);            new Thread(futureTask).start();            String result = futureTask.get();            System.out.println(result);        }        System.out.println("测试第四部分 Runnable+CachedThreadPool");        for (Integer i = 0; i < 5; i++) {            ExecutorService executorService = Executors.newCachedThreadPool();            TestRunnable testRunnable = new TestRunnable("第四部分 ", i);            executorService.submit(testRunnable);        }        System.out.println("测试第五部分 Runnable+CachedThreadPool");        for (Integer i = 0; i < 5; i++) {            ExecutorService executorService = Executors.newCachedThreadPool();            TestCallable testCallable = new TestCallable("第五部分 ", i);            Future<String> future = executorService.submit(testCallable);            System.out.println(future.get());        }        System.out.println("测试第六部分 Callable+CompletionService+CachedThreadPool");        ExecutorService executorService = Executors.newCachedThreadPool();        CompletionService<String> completionService = new ExecutorCompletionService<String>(executorService);        for (Integer i = 0; i < 5; i++) {            TestCallable testCallable = new TestCallable("第六部分 ", i);            completionService.submit(testCallable);        }        for (Integer i = 0; i < 5; i++) {            Future<String> future = completionService.take();            System.out.println(future.get());        }        System.out.println("End");    }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918