• 概述
    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");
    }
}

Java线程基础实例的更多相关文章

  1. Java 线程基础

    Java 线程基础

  2. Java线程基础知识(状态、共享与协作)

    1.基础概念 CPU核心数和线程数的关系 核心数:线程数=1:1 ;使用了超线程技术后---> 1:2 CPU时间片轮转机制 又称RR调度,会导致上下文切换 什么是进程和线程 进程:程序运行资源 ...

  3. java线程基础知识----线程与锁

    我们上一章已经谈到java线程的基础知识,我们学习了Thread的基础知识,今天我们开始学习java线程和锁. 1. 首先我们应该了解一下Object类的一些性质以其方法,首先我们知道Object类的 ...

  4. java线程基础巩固---线程生命周期以及start方法源码剖析

    上篇中介绍了如何启动一个线程,通过调用start()方法才能创建并使用新线程,并且这个start()是非阻塞的,调用之后立马就返回的,实际上它是线程生命周期环节中的一种,所以这里阐述一下线程的一个完整 ...

  5. Java 线程基础知识

    前言 什么是线程?线程,有时被称为轻量进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程 ID,当前指令指针 (PC),寄存器集合和堆栈组成.另外,线 ...

  6. 线程之一:JAVA线程基础

    参考core java,马士兵视频 1.线程的基本概念 (1)一个线程是一个程序内部的顺序控制流. (2)线程和进程 –每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大. –线程:轻量 ...

  7. java线程池实例

    目的         了解线程池的知识后,写个线程池实例,熟悉多线程开发,建议看jdk线程池源码,跟大师比,才知道差距啊O(∩_∩)O 线程池类 package thread.pool2; impor ...

  8. java线程基础知识----线程基础知识

    不知道从什么时候开始,学习知识变成了一个短期记忆的过程,总是容易忘记自己当初学懂的知识(fuck!),不知道是自己没有经常使用还是当初理解的不够深入.今天准备再对java的线程进行一下系统的学习,希望 ...

  9. 线程之一:JAVA线程基础 分类: B1_JAVA 2013-10-10 12:48 662人阅读 评论(0) 收藏

    参考core java,马士兵视频 1.线程的基本概念 (1)一个线程是一个程序内部的顺序控制流.   (2)线程和进程 –每个进程都有独立的代码和数据空间(进程上下文),进程切换的开销大. –线程: ...

随机推荐

  1. 简单设置eworkflow条件的方式

    在eworkflow自定义工作流产品中,设置条件节点,是在节点的后续连线上设置的.每一个处理节点(除结束节点外)都至少有一条连线连接到下一个节点,当有多条连线连接到其他节点的时候,就需要在多出的连线上 ...

  2. 扩展 HtmlwebpackPlugin 插入自定义的脚本

    webpack提供了一个如何开发 webpack 插件的介绍,你可以直接访问这里查看,这里提供一个扩展 HtmlWebpackPlugin 的开发实例. 前面我们介绍过 HtmlWebpackPlug ...

  3. ebs如何将客户化的PL/SQL程序发布到webservice

    as:cux_gl_hec_iface_soa_pkg. 1.将package声明部分的内容拷贝出来另存为cux_gl_hec_iface_soa_pkg.pls的文件: 2.将该文件上传到服务器上拥 ...

  4. ionic 里使用 iframe 可能遇到的问题

    无法访问外部url的问题--两个步骤解决: iframe的src属性用ng-src属性替代,并指明绑定对象: ng-src="{{targetUrl}}" 在controller里 ...

  5. 虚拟机中CentOS 7下PHP环境配置

    为了简单起见,虚拟机网卡设置为桥接模式 1.yum install  httpd php 2.yum install mariadb 3.启动mariadb systemctl start maria ...

  6. Ping出现TTL expired in transit的原因

    今天上班检查服务器时发现出现TTL expired in transit的提示,这种提示还是第一次见,如图: ping 和 tracert 的结果 用tracert来看路由状况,原来是出现路由环路导致 ...

  7. Bash注释

    1)单行注释: #!/bin/bash #echo "Martin" echo "Martin" #echo "Martin" 2)多行注释 ...

  8. td的所有style

    td.style.clear= td.style.posRight=0 td.style.backgroundRepeat= td.style.borderTopStyle= td.style.mar ...

  9. nodejs+express+jade配置

    安装步骤 一.首先可跟着这个网址安装http://jingyan.baidu.com/article/91f5db1b2bb6941c7f05e33c.html,路径可由自己定. 二.同时参考http ...

  10. JS函数

    1.document.write(""); 输出语句2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个浏 ...