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

最新文章

  1. Caffe使用:如何将一维数据或其他非图像数据转换成lmdb
  2. 原生的强大DOM选择器querySelector
  3. atitit。自定义uml MOF EMF体系eclipse emf 教程o7t
  4. Android中四种OnClick事件的写法
  5. 软技能:十步学习法 (zhuan)
  6. python一些模块的exe安装包在windows的64位系统里识别不到已安装Python目录的解决方法
  7. Spring Boot Admin Reference Guide
  8. 中国IT职业培训市场经历的几波浪潮,未来的浪潮又是那一波?
  9. ipcs、ipcrm、sysresv、kernel.shmmax
  10. .NET Core快速入门教程 5、使用VS Code进行C#代码调试的技巧
  11. ubuntu远程windows桌面
  12. JavaScript 平时记录
  13. 关于java文件下载文件名乱码问题解决方案
  14. vue修饰符学习
  15. mysql 第二高薪水
  16. 001-RESTful服务最佳实践-RestFul准则、HTTP动词表示含义、合理的资源命名、响应格式XML和JSON
  17. 总结docker常用命令
  18. Mac安装compass失败的原因
  19. pycharm跳到指定的行
  20. python-day64--web框架

热门文章

  1. LeetCode--142--环形链表II(python)
  2. 【bzoj3277&amp;&amp;3474】串
  3. Build安装版的UE4
  4. IE等浏览器兼容问题解决方案
  5. [CSP-S模拟测试]:环(图论+期望)
  6. [CSP-S模拟测试]:毛三琛(随机化+二分答案)
  7. [BZOJ2038]:[2009国家集训队]小Z的袜子(hose)(离线莫队)
  8. Ecipse代码调试
  9. 基于python实现自动化办公学习笔记四
  10. spark 学习网站和资料