Java的并发是在顺序语言的基础上提供对线程的支持的。

并发能够更加有效的执行我们的代码,也就是更加合理的应用CPU资源。

并发程序往往CPU和内存使用率,要高于同等的非并发程序。

下面就用Think_in_java_4th,并发这个章节中源码简单说一下自己的认识。

LiftOff.java

package concurrency;

//: concurrency/LiftOff.java
// Demonstration of the Runnable interface. public class LiftOff implements Runnable {
protected int countDown = 10; // Default
private static int taskCount = 0;
private final int id = taskCount++; public LiftOff() {
} public LiftOff(int countDown) {
this.countDown = countDown;
} public String status() {
return "#" + id + "(" + (countDown > 0 ? countDown : "Liftoff!") + "), ";
} public void run() {
while (countDown-- > 0) {
System.out.print(status());
//线程调度器
//将CPU从一个线程转移給另一个线程。
Thread.yield();
}
}
} /// :~

MainThread.java

package concurrency;

//: concurrency/MainThread.java

public class MainThread {
public static void main(String[] args) {
LiftOff launch = new LiftOff();
launch.run();
}
} /*
* Output: #0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1),
* #0(Liftoff!),
*/// :~

MainThread的执行结果和我们不使用多线程基本上没有什么区别。

package concurrency;

//: concurrency/BasicThreads.java
// The most basic use of the Thread class. public class BasicThreads {
public static void main(String[] args) {
Thread t = new Thread(new LiftOff());
t.start();
System.out.println("Waiting for LiftOff");
}
} /*
* Output: (90% match) Waiting for LiftOff #0(9), #0(8), #0(7), #0(6), #0(5),
* #0(4), #0(3), #0(2), #0(1), #0(Liftoff!),
*/// :~
Thread 类使用后,我们可以看到。【"Waiting for LiftOff"】比【#0(9),...ff!),】更早被打印出来了。
此时,CPU和内存的使用情况就初步体现出来了。
new Thread的JDK源码。
target(实现了Runnable的类)最终被给了一个新的tid(nextThreadID)之后,加入到ThreadGroup中。 Thread
    public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
    private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc) {
。。。。。。
g.addUnstarted();
。。。。。。
this.target = target;
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =
ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize; /* Set thread ID */
tid = nextThreadID();
}

好了,我们大概知道new Thread 的过程了。此处应该有图,哈哈。偷个懒。

也就是说,在【new Thread(new LiftOff())】被给予新的tid并加入到ThreadGroup的时候,System.out执行了。

并且,System.out没有等待【t.start();】的执行完,再开始执行。

这样的结果就是因为,主线程和子线程并发执行的结果。

这时,【在顺序语言的基础上提供对线程的支持】句话就可以拿出来再理解一下了。

Thread

    public synchronized void start() {
......
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
......
}

ThreadGroup

    void add(Thread t) {
......
nUnstartedThreads--;
......
}

MoreBasicThreads.java

package concurrency;

//: concurrency/MoreBasicThreads.java
// Adding more threads. public class MoreBasicThreads {
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
new Thread(new LiftOff()).start();
System.out.println("Waiting for LiftOff");
}
} /*
* Output: (Sample) Waiting for LiftOff #0(9), #1(9), #2(9), #3(9), #4(9),
* #0(8), #1(8), #2(8), #3(8), #4(8), #0(7), #1(7), #2(7), #3(7), #4(7), #0(6),
* #1(6), #2(6), #3(6), #4(6), #0(5), #1(5), #2(5), #3(5), #4(5), #0(4), #1(4),
* #2(4), #3(4), #4(4), #0(3), #1(3), #2(3), #3(3), #4(3), #0(2), #1(2), #2(2),
* #3(2), #4(2), #0(1), #1(1), #2(1), #3(1), #4(1), #0(Liftoff!), #1(Liftoff!),
* #2(Liftoff!), #3(Liftoff!), #4(Liftoff!),
*/// :~
#0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #1(9), #2(9), #1(8), #0(2), #3(9), #1(7), Waiting for LiftOff
#3(8), #3(7), #3(6), #3(5), #3(4), #3(3), #3(2), #3(1), #3(Liftoff!), #1(6), #1(5), #1(4), #1(3), #1(2), #1(1), #1(Liftoff!), #2(8), #2(7), #2(6), #2(5), #2(4), #2(3), #2(2), #2(1), #2(Liftoff!), #0(1), #4(9), #0(Liftoff!), #4(8), #4(7), #4(6), #4(5), #4(4), #4(3), #4(2), #4(1), #4(Liftoff!),

创建多个线程,并启动。

实际上,线程的创建销毁是非常快的。当一个线程完成了它的任务的时候,该线程占有的资源就会被释放。

那么我们就又可以把已经释放的资源用于创建其他新的线程了。

参考

Java编程思想(第4版)    654页开始

Thinking in Java(第四版 )  1116页开始

Think_in_java_4th(并发学习一)的更多相关文章

  1. C++11并发学习之三:线程同步(转载)

    C++11并发学习之三:线程同步 1.<mutex> 头文件介绍 Mutex又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文 ...

  2. 【Todo】Java并发学习 & 示例练习及代码

    接上一篇:http://www.cnblogs.com/charlesblc/p/6097111.html <Java并发学习 & Executor学习 & 异常逃逸 & ...

  3. Java并发学习(一):进程和线程

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 俗话说得好"一人 ...

  4. Think_in_java_4th(并发学习二)

    使用Executor java.util.concurrent CachedThreadPool package concurrency.ExecutorService; //: concurrenc ...

  5. Java并发学习之十九——线程同步工具之Phaser

    本文是学习网络上的文章时的总结.感谢大家无私的分享. JDK 1.7 加入了一个新的工具Phaser.Phaser的在功能上与CountDownLatch有部分重合. 以下使用Phaser类来同步3个 ...

  6. Java多线程高并发学习笔记(一)——Thread&Runnable

    进程与线程 首先来看百度百科关于进程的介绍: 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动.它可以申请和拥有系统资源,是一个动态的概念,是一个活动的实体.它不只是程序的代码,还包括当前的 ...

  7. Java多线程高并发学习笔记——阻塞队列

    在探讨可重入锁之后,接下来学习阻塞队列,这边篇文章也是断断续续的写了很久,因为最近开始学ssm框架,准备做一个自己的小网站,后续可能更新自己写网站的技术分享. 请尊重作者劳动成果,转载请标明原文链接: ...

  8. python并发学习总结

    目录 一.理解操作系统 二.任务类型 三.Socket模块 四.一个简单的C/S程序 五.使用阻塞IO实现并发 方案一:阻塞IO+多进程 方案二:阻塞IO+多线程 阻塞IO模型的思考和总结 六.使用非 ...

  9. c++多线程并发学习笔记(0)

    多进程并发:将应用程序分为多个独立的进程,它们在同一时刻运行.如图所示,独立的进程可以通过进程间常规的通信渠道传递讯息(信号.套接字..文件.管道等等). 优点:1.操作系统在进程间提供附附加的保护操 ...

随机推荐

  1. Python—day17时间模块、系统模块、递推遍历、序列化

    一.time'''时间戳(timestamp):time.time()延迟线程的运行:time.sleep(secs)(指定时间戳下的)当前时区时间:time.localtime([secs])(指定 ...

  2. python 菱形继承问题究极版

    如果只是正常的菱形继承,经典类(python2中最后一个父类不继承object类)是深度优先,即会从左边父类开始一路走到底 新式类(最后一个父类继承了object类)是广度优先,即从左边父类开始继承, ...

  3. Python内置函数(68)——__import__

    英文文档: __import__(name, globals=None, locals=None, fromlist=(), level=0) This function is invoked by ...

  4. Asp.Net Core WebApi (Swagger+EF Core/Code First)

    Swagger简介: Swagger™的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能. ...

  5. https和http共存的nginx简单配置

    server{ listen 80; listen 443 ssl; ssl_certificate /usr/local/nginx/ssl/www.demo.com/www.demo.com.cn ...

  6. 探索ASP.NET Core中的IStartupFilter

    原文:Exploring IStartupFilter in ASP.NET Core 作者:Andrew Lock 译者:Lamond Lu 在本篇博客中,我将介绍一下IStartupFilter, ...

  7. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  8. GraphQL 的前世今生

    GraphQL是什么 GraphQL是一种新的API标准,它提供了一种更高效.强大和灵活的数据提供方式.它是由Facebook开发和开源,目前由来自世界各地的大公司和个人维护.GraphQL本质上是一 ...

  9. leetcode — symmetric-tree

    import java.util.Stack; /** * Source : https://oj.leetcode.com/problems/symmetric-tree/ * * * Given ...

  10. [解决方案] Ubuntu 16.04 下 Qt 5.6 无法输入中文的问题

    0. 环境 系统:ubuntu 16.04 LTS 机子:dell xps13 1. 步骤 1.1 编译 fcitx-qt5 源码 编译fcitx-qt需要cmake,安装cmake命令,如果已经安装 ...