Java并发知识总结
jixu
8. 并发
启动线程的几种方式
Thread t7 = new Thread(timer);
t7.start();
Thread.sleep(100) //暂停当前线程
class MT extends Thread {
private int n;
public MyThread( int n ){
super();
this.n=n;
}
public void run() {
for(int i=0;i<n;i++) {
System.out.println(i);
}
}
} Class MT implements Runnable{
private int n;
public MyTask(int n){
this.n = n;
}
public void run() {
for(int i=0; i<n; i++) {
System.out.println(i);
}
}
} new Thread(){
public void run() {
for(int i=0; i<10; i++)
System.out.println(i);
}
}.start(); new Thread( ( ) -> {
for(int i=0; i<10; i++)
System.out.println(i);
} ).start();
线程同步
- synchronize:对象加锁
synchronized(cnt) {
cnt++; //临界区
}- wait() 释放对象锁,阻塞当前进程
- notify() / notifyAll() 相当于signal,让阻塞的线程继续
java并发API:java.util.concurrent
原子变量:保证线程安全
AtomicInteger cnt=new AtomicInteger(0);
cnt.getAndIncrement();
集合:
ArrayList/HashMap不是线程安全的
Vector及Hashtable是线程安全的
CopyOnWriteArrayList、 CopyOnWriteArraySet:适合于很少写入而读取频繁的对象
ConcurrentHashMap:putIfAbsent(), remove(), replace()
队列:
BlockingQueue<Integer> q=new ArrayBlockingQueue<>(3); put() take()
线程池
class ThreadPoolDemo
{
public static void main(String[] args) {
ExecutorService pool=Executors.newCachedThreadPool();
MyTask t1=new MyTask(5); MyTask t2=new MyTask(7);
pool.execute(t1); pool.execute(t2);
pool.shutdown();
}
}
class MyTask implements Runnable {
int n=10;
public MyTask(int n){ this.n=n;}
public void run(){
for(int i=0;i<n; i++)System.out.print(i);
}
}
实现一个生产者-消费者问题
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i <10; i++)
cubbyhole.put(i);
}
} class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i <10; i++)
value = cubbyhole.get();
}
} class CubbyHole {
private int data[] = new int[3];
private int index = 0;
public synchronized int get() {
while (index <= 0) {
try{
wait(); //waits for notify() from Producer
} catch (InterruptedException e) { }
}
index --;
int value = data[index];
System.out.println("Consumer got: " + data[index]);
notify();
return value;
}
public synchronized void put(int value) {
while (index >= data.length) {
try{
wait(); //waits for notify() from consumer
} catch (InterruptedException e) { }
}
System.out.println("Producer put: " + value);
data[index] = value;
index ++;
notify();
}
} class ProducerConsumerStack {
public static void main(String args[]) {
CubbyHole c = new CubbyHole();
Producer p1=new Producer(c,1);
Consumer c1=new Consumer(c,1);
p1.start();
c1.start();
}
}
Ref:https://github.com/CyC2018/CS-Notes/blob/master/notes/Java%20%E5%B9%B6%E5%8F%91.md
Java并发知识总结的更多相关文章
- Java并发知识整理
整理了一下前段时间学习Java并发的笔记,大约有40篇. 1. Java并发基础知识 并发基础(一) 线程介绍 并发基础(二) Thread类的API总结 并发基础(三) java线程优先级 并发基础 ...
- Java并发知识总结,超详细!
首先给大家分享一个github仓库,上面放了200多本经典的计算机书籍,包括C语言.C++.Java.Python.前端.数据库.操作系统.计算机网络.数据结构和算法.机器学习.编程人生等,可以sta ...
- Java并发知识(1)
1. 进程和线程之间有什么不同? 一个进程是一个独立(self contained)的运行环境,它可以被看作一个程序或者一个应用.而线程是在进程中执行的一个任务.Java运行环境是一个包含了不同的类和 ...
- Java并发知识(2)
1. 什么是原子操作?在Java Concurrency API中有哪些原子类(atomic classes)? 原子操作是指一个不受其他操作影响的操作任务单元.原子操作是在多线程环境下避免数据不一致 ...
- Java并发知识分享
volatile的内存语义 从JSR-133(即从JDK1.5开始),volatile变量的写-读可以实现线程之间的通信 当写一个volatile变量时,JMM会把该线程对应的本地内存中的共享变量值刷 ...
- Java并发知识概述
1.Java内存模型的抽象结构 Java中,所有的实例.静态域和数组元素都存储在堆内存中,堆内存是线程共享的.局部变量,形参,异常处理参数不会在线程之间共享,所以不存在内存可见性问题,也就不受内存模型 ...
- (一)java并发知识图谱
- java完整并发知识结构图
一张大的java并发知识结构图,梳理清楚知识的脉络,知识不再零散
- Java并发编程有多难?这几个核心技术你掌握了吗?
本文主要内容索引 1.Java线程 2.线程模型 3.Java线程池 4.Future(各种Future) 5.Fork/Join框架 6.volatile 7.CAS(原子操作) 8.AQS(并发同 ...
随机推荐
- mybaits 时间查询DATE_FORMAT
<if test="accountdayInout.inoutDateStart!=null"> and DATE_FORMAT(t.inout_date,'%Y-%m ...
- PHP培训教程 PHP的运算符
PHP中有丰富的运算符集,它们中大部分直接来自于C语言.按照不同功能区分,兄弟连PHP培训 运算符可以分为:算术运算符.字符串运算符.赋值运算符.位运算符.条件运算符,以及逻辑运算符等.当各种运算符在 ...
- GeoServer-2.12安装MbTiles扩展插件
- 手写Tomcat源码
http://search.bilibili.com/all?keyword=%E6%89%8B%E5%86%99Tomcat%E6%BA%90%E7%A0%81 tomcat源码分析一:https: ...
- 【30分钟学完】canvas动画|游戏基础(2):从零开始画画
前言 上篇主要是理论的概述,本篇会多些实践,来讲讲canvas的基础用法,并包含一些基础三角函数的应用,推荐没有canvas基础的朋友阅读,熟悉的朋友可以跳过. 本人能力有限,欢迎牛人共同讨论,批评指 ...
- Hash算法原理以及HashCode深入理解
Java中的Collection有两类,一类是List,一类是Set.List内的元素是有序的,元素可以重复.Set元素无序,但元素不可重复.要想保证元素不重复,两个元素是否重复应该依据什么来判断呢? ...
- ES6 对象超类
var parent = { foo() { console.log("Hello from the Parent"); } } var child = { foo() { sup ...
- Matlab 读取文件夹里所有的文件
(image = dir('D:\gesture\*.*'); % dir是指定文件夹得位置,他与dos下的dir用法相同. 用法有三种: 1. dir 是指工作在当前文件夹里 2. dir name ...
- fedora23使用Xwayland的gnome-shell
gnome是桌面管理系统的名称, 包括gnome, kde, xfce等等 同时, gnome是旧的gnome 2 的桌面管理 在gnome 3中, 桌面管理系统叫做gnome shell. gnom ...
- iView 实战系列教程(21课时)_3.iView 实战教程之布局篇(一)
Grid布局 先了解一下iview的24栅格布局 清理一下App.vue 然后从iview的color里面获取推荐的背景色 我们先渲染栅格 24列,然后再讲解他是一个什么东西: 栅格外面row包裹的, ...