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. 记一次尴尬的git reset丢失分支故障

    最近...似乎一直在踩坑... 也不是什么故障,只是把一个分支的功能弄没了,之后在reflog里找到又恢复了. 产生原因是有同事错误地把分支B merge到了分支A并push. 我直接在分支A上res ...

  2. 10.Django ModelForm

    ModelForm 1.ModeForm简单验证 from django.db import models # Create your models here. class UserInfo(mode ...

  3. wp8使用Beetle.NetPackage实现基于TCP通讯的订单查询

    在新版本的Beetle.NetPackage中提供了对Protobuf和Controller的支持,所以在WP8下使用Beetle.NetPackage进行基于TCP的数据交互则一件非常简单事情.下面 ...

  4. linux进程管理和系统状态常用命令简介

    1 进程管理简介 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础 2 常用命令 2.1 pstree 2.1.1 功能描 ...

  5. Android总结篇系列:Activity启动模式(lauchMode)

    本来想针对Activity中的启动模式写篇文章的,后来网上发现有人已经总结的相当好了,在此直接引用过来,并加上自己的一些理解,在此感谢原作者. 文章地址: http://blog.csdn.net/l ...

  6. jmeter 分布式压测(Linux)

    之前一篇博文写的是如何在Linux上使用jmeter压测,这篇介绍下Linux上jmeter的分布式压测. 和windows上的分布式类似,需要配置agent节点和控制机 一.Agent节点配置 1. ...

  7. ASP.NET Core教程【二】从保存数据看Razor Page的特有属性与服务端验证

    前文索引:ASP.NET Core教程[一]关于Razor Page的知识 在layout.cshtml文件中,我们可以看到如下代码: <a asp-page="/Index" ...

  8. 深度学习(Deep Learning)资料大全(不断更新)

    Deep Learning(深度学习)学习笔记(不断更新): Deep Learning(深度学习)学习笔记之系列(一) 深度学习(Deep Learning)资料(不断更新):新增数据集,微信公众号 ...

  9. App阅读pdf和扫描二维码功能

    在之前开发的Android手机App中,需要实现阅读pdf和扫描二维码的功能,在github 上找到大牛封装好包,亲测可用. 阅读pdf: https://github.com/barteksc/An ...

  10. .NET读取json数据并绑定到对象

    需要引用的命名空间: 读取的具体应用: this代表本实体(对象),通过PopulateObject,直接将读取到的json数据与对象进行绑定 Json保存的具体应用: 将对象保存为Json JObj ...