java 多线程和并行程序设计
多线程使得程序中的多个任务可以同时执行
在一个程序中允许同时运行多个任务。在许多程序设计语言中,多线程都是通过调用依赖系统的过程或函数来实现的





为什么需要多线程?多个线程如何在单处理器系统中同时运行?
多线程可以使您的程序更具响应性和交互性,并提高性能。在许多情况下需要多线程,例如动画和客户端/服务器计算。因为大多数时候CPU处于空闲状态 - 例如,CPU在用户输入数据时什么都不做 - 多个线程在单处理器系统中共享CPU时间是切实可行的。
1、创建任务和线程
一个任务类必须实现Runnable接口。任务必须从线程运行。

一旦定义了一个TaskClass,就可以用它的构造方法创建一个任务。
例子:
TaskClass task = new TaskClass();
任务必须在线程中执行。使用下面的语句创建任务的线程
Thread thread = new Thread(task);
然后调用start()方法告诉Java虚拟机该线程准备运行
thread.start()
例子:

public class TaskThreadDemo {
public static void main(String [] args) {
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable printNum = new PrintNum(100);
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(printNum);
thread1.start();
thread2.start();
thread3.start();
}
}
class PrintChar implements Runnable{
private char charToPrint;
private int times;
public PrintChar(char c,int t) {
charToPrint = c;
times = t;
}
@Override
public void run() {
for(int i=0; i<times; i++) {
System.out.print(charToPrint);
}
}
}
class PrintNum implements Runnable{
private int lastNumber;
public PrintNum(int n) {
lastNumber = n;
}
@Override
public void run() {
for(int i=1; i<=lastNumber; i++) {
System.out.print(" " + i);
}
}
}
2、Thread类









3、线程池
之前运用实现Runnable接口来定义一个任务类,以及如何创建一个线程来运行一个任务

该方法对大量的任务而言是不够高效的,为每个任务开始一个新线程可能会限制吞吐量并且造成性能降低。
线程池是管理并发执行任务个数的理想方法。
Java提供Executor接口来执行线程池中的任务,提供ExecutorService接口来管理和控制任务。
Executorservice是Executor的子接口

为了创建一个Executor对象,可以使用Executor类中的静态方法


例子:
public class TaskThreadDemo {
public static void main(String [] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
// ExecutorService executor = Executors.newCachedThreadPool();
executor.execute(new PrintChar('a', 100));
executor.execute(new PrintChar('b', 100));
executor.execute(new PrintNum(100));
executor.shutdown();
}
}
4、线程同步
例子:
public class AccountWithoutSync {
private static Account account = new Account();
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 100; i++) {
executorService.execute(new AddAPennyTask());
}
executorService.shutdown();
//等待 全部任务 完成
while(!executorService.isTerminated()) {
}
System.out.println(account.getBalance());
}
private static class AddAPennyTask implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
account.deposit(1);
}
}
public static class Account {
private int balance = 0;
public int getBalance() {
return balance;
}
public void deposit(int amount) {
int newBalance = balance + amount;
try {
Thread.sleep(5);
}catch (InterruptedException e) {
// TODO: handle exception
}
balance = balance + newBalance;
}
}
}

这个例子中出错了
原因是:

因为线程不安全,所以数据遭到破坏
synchronized关键字
为避免竞争状态,应该防止多个线程同时进入程序的某一特定部分,程序中的这部分称为临界区。
使用关键字synchronized来同步方法,以便一次只有一个线程可以访问这个方法
例子:



调用一个对象上的同步实例方法,需要给该对象加锁。而调用一个类上的同步静态方法,需要给该类加锁。
同步语句
当执行方法中某一个代码块时,同步语句不仅可用与this对象加锁,而且可用于对任何对象加锁。这个代码块称为同步块。同步语句的一般形式如下:

利用加锁同步
之前的用的同步实例方法

实际上 在执行之前都隐式地需要一个加在实例上的锁



例子:
部分代码:
public static class Account {
private int balance = 0;
private Lock lock = new ReentrantLock(); //创建一个锁
public int getBalance() {
return balance;
}
public void deposit(int amount) {
lock.lock();//获取该锁
try {
int newBalance = balance + amount;
Thread.sleep(5);
balance = newBalance;
}catch (InterruptedException e) {
// TODO: handle exception
}finally {
lock.unlock();//释放该锁
}
}
}
线程间协作


例子:

public class ThreadCooperation {
private static Account account = new Account();
public static void main(String [] args) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new DepositTask());
executorService.execute(new withdrawTask());
executorService.shutdown();
System.out.println("Thread 1 \t Thread 2 \t Balance");
System.out.println();
}
public static class DepositTask implements Runnable{
@Override
public void run() {
try {
while(true) {
account.deposit((int)(Math.random() * 10) + 1);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static class withdrawTask implements Runnable{
@Override
public void run() {
while(true) {
account.withdraw((int)(Math.random() * 10) + 1);
}
}
}
public static class Account {
private static Lock lock = new ReentrantLock();
private static Condition newDeposit = lock.newCondition(); //线程间的协作
private int balance = 0;
public int getBalance() {
return balance;
}
public void withdraw(int accountNumber) {
lock.lock();
try {
while(balance < accountNumber) {
System.out.println("\t\tWait for a deposit, wait to withdraw :" + accountNumber);
newDeposit.await();
}
balance -= accountNumber;
System.out.println("\t\tWithdraw: " + accountNumber + "\t\t" + "balance: " + getBalance());
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void deposit(int accountNumber) {
lock.lock();
try {
balance += accountNumber;
System.out.println("Deposit " + accountNumber + "\t\t\t\t balance: " + getBalance());
newDeposit.signalAll();
}
finally {
lock.unlock();
}
}
}
}



消费者/生产者


public class ComsumerProducer {
private static Buffer buffer = new Buffer();
public static void main(String [] args) {
ExecutorService excurtor = Executors.newFixedThreadPool(2);
excurtor.execute(new ProducerTask());
excurtor.execute(new ConsumerTask());
excurtor.shutdown();
}
private static class ProducerTask implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
int i = 1;
while(true) {
System.out.println("Producer writes " + i);
buffer.write(i++);
Thread.sleep((int)(Math.random() * 1000));
}
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
private static class ConsumerTask implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(true) {
System.out.println("\t\tConsumer reads " + buffer.read());
Thread.sleep((int)(Math.random() * 1000));
}
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
private static class Buffer{
private static final int CAPACITY = 3;
private LinkedList<Integer> queue = new LinkedList<>();
private static Lock lock = new ReentrantLock();
private static Condition notFull = lock.newCondition();
private static Condition notEmpty = lock.newCondition();
public void write(int value) {
lock.lock();
try{
while(queue.size() == CAPACITY) {
System.out.println("Wait for notNull condition");
notFull.await();
}
queue.offer(value);
notEmpty.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
lock.unlock();
}
}
public int read() {
lock.lock();
int value = 0;
try {
while(queue.isEmpty()) {
System.out.println("\t\tWait for notEmpty condition");
notEmpty.await();
}
value = queue.pop();
notFull.signal();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
lock.unlock();
return value;
}
}
}
}

阻塞队列

阻塞队列在试图向一个满队列添加元素或者从空队列中删除元素时会导致线程阻塞。BlockingQueue接口继承了Queue,并且提供同步的put和take方法向队列尾部添加元素,以及从队列头部删除元素

java支持三个具体的阻塞队列ArrayBlockingQueue、LinkedBlockingQueue和PriorityBlockingQueue




例子:
用阻塞队列做的消费者/生产者
public class ConsumerProducerUsingBlockingQueue {
private static ArrayBlockingQueue<Integer> buffer = new ArrayBlockingQueue<Integer>(2);
public static void main(String [] args) {
ExecutorService excutor = Executors.newFixedThreadPool(2);
excutor.execute(new ProducerTask());
excutor.execute(new ConsumerTask());
excutor.shutdown();
}
private static class ProducerTask implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
int i = 1;
while(true) {
System.out.println("Producer writes " + i);
buffer.put(i++);
Thread.sleep((int)(Math.random() * 1000));
}
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
private static class ConsumerTask implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(true) {
System.out.println("\t\tConsumer reads " + buffer.take());
Thread.sleep((int)(Math.random() * 1000));
}
}catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}

信号量



例子:


同步合集


并行编程

java 多线程和并行程序设计的更多相关文章
- [Java多线程]-并发,并行,synchonrized同步的用法
一.多线程的并发与并行: 并发:多个线程同时都处在运行中的状态.线程之间相互干扰,存在竞争,(CPU,缓冲区),每个线程轮流使用CPU,当一个线程占有CPU时,其他线程处于挂起状态,各线程断续推进. ...
- JAVA多线程---高并发程序设计
先行发生原则 程序顺序原则:一个线程内保证语义的串行性 volatile:volatile变量的写,先发生于读,这保证了volatile变量的可见性 锁规则:解锁必然发生在随后的加锁前 传递性:A优先 ...
- Java多线程程序设计详细解析
一.理解多线程 多线程是这样一种机制,它允许在程序中并发执行多个指令流,每个指令流都称为一个线程,彼此间互相独立. 线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线 ...
- Java多线程--并行模式与算法
Java多线程--并行模式与算法 单例模式 虽然单例模式和并行没有直接关系,但是我们经常会在多线程中使用到单例.单例的好处有: 对于频繁使用的对象可以省去new操作花费的时间: new操作的减少,随之 ...
- 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\
1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...
- Java多线程原理+基础知识(超级超级详细)+(并发与并行)+(进程与线程)1
Java多线程 我们先来了解两个概念!!!! 1.什么是并发与并行 2.什么是进程与线程 1.什么是并发与并行 1.1并行:两个事情在同一时刻发生 1.2并发:两个事情在同一时间段内发生 并发与并行的 ...
- Java多线程专题1: 并发与并行的基础概念
合集目录 Java多线程专题1: 并发与并行的基础概念 什么是多线程并发和并行? 并发: Concurrency 特指单核可以处理多任务, 这种机制主要实现于操作系统层面, 用于充分利用单CPU的性能 ...
- 从JAVA多线程理解到集群分布式和网络设计的浅析
对于JAVA多线程的应用非常广泛,现在的系统没有多线程几乎什么也做不了,很多时候我们在何种场合如何应用多线程成为一种首先需要选择的问题,另外关于java多线程的知识也是非常的多,本文中先介绍和说明一些 ...
- 学习笔记之JAVA多线程
Java程序设计实用教程 by 朱战立 & 沈伟 孙鑫Java无难事 Java 多线程与并发编程专题(http://www.ibm.com/developerworks/cn/java/j-c ...
随机推荐
- BZOJ5300 [Cqoi2018]九连环 【数学】【FFT】
题目分析: 这道题是数学必修五的原题,做法如下图,书上讲得很详细了. 那么这道题目用快速幂就可以解决了,值得注意的是,分析时间复杂度会发现直接做乘法其实是O(n^2)的,但是有一个1/20左右的常数, ...
- MT【234】正方形染色(二)
有$n$个正方形排成一行,今用红,白,黑三种颜色给这$n$个正方形染色,每个正方形只能染一种颜色.如果要求染这三种颜色的正方形都是偶数个,问有多少种不同的染色方法. 解答: 设有$a_n$种不同的染法 ...
- 自学Zabbix4.2.1 Application介绍
自学Zabbix4.2.1 Application介绍 Applications应用程序是item的一个组.例如我们要监控MySQL,我们可以将所有和MySQL相关的item放到这个应用程序中.例如M ...
- JDK源码分析(1)ArrayList
JDK版本 ArrayList简介 ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomA ...
- activity之间的数据传递方法
1 基于消息的通信机制 Intent--------boudle,extra 用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int.string等 详细介绍下Intent机制 Inte ...
- Java关键字synchronized详解
Java多线程thread互联网制造 synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程A每次运行到这个方法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话 ...
- SIEVE 线性筛
今天来玩玩筛 英文:Sieve 有什么筛? 这里介绍:素数筛,欧拉筛,约数个数筛,约数和筛 为什么要用筛? 顾名思义,筛就是要漏掉没用的,留下有用的.最终筛出来1~n的数的一些信息. 为什么要用线性筛 ...
- A1038. Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 【POJ3585】Accumulation Degree 二次扫描与换根法
简单来说,这是一道树形结构上的最大流问题. 朴素的解法是可以以每个节点为源点,单独进行一次dp,时间复杂度是\(O(n^2)\) 但是在朴素求解的过程中,相当于每次都求解了一次整棵树的信息,会做了不少 ...
- 【Python】统计个人新浪微博词频并给出相应的柱状图
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...