代码:chapter9
sleep:是Thread的方法,sleep不释放锁,sleep不用synchronized,不需要被唤醒。
wait:所有对象的方法,wait释放锁,
用synchronized,要被唤醒。
如何使用这个案例:切换m1和m2方法。
chapter9
package one.chapter9;
import java.util.stream.Stream;
public class DifferenceOfWaitAndSleep {
private final static Object LOCK = new Object(); // 定义锁一定要定义final类型的
public static void main(String[] args) {
Stream.of("T1", "T2").forEach(name ->
new Thread(name) {
@Override
public void run() {
m1();
}
}.start()
);
}
public static void m1() {
synchronized (LOCK) {
try {
System.out.println("The Thread " + Thread.currentThread().getName() + " enter.");
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void m2() {
synchronized (LOCK) {
try {
System.out.println("The Thread " + Thread.currentThread().getName() + " enter.");
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
----------------------------------------27-----------sleep和wait的区别-------------------
先start之后才可以join。先设置守护线程才可以start。
线程的数量的优化图,多了上下文就多了会有开销的。
卡了看看是不是线程太多了。
jdk8的::https://blog.csdn.net/csmans/article/details/82256690
---
问题:10000台机器如何用100个线程采集,线程多了会有问题,上下文切换。
代码:
package one.chapter9;
import java.util.*;
public class CaptureService {
final static private LinkedList<Control> CONTROLS = new LinkedList<>();
private final static int MAX_WORKER = 5;
public static void main(String[] args) {
List<Thread> worker = new ArrayList<>();
Arrays.asList("M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10").stream()
.map(CaptureService::createCaptureThread)
.forEach(t -> {
t.start();
worker.add(t);
});
// 在这里去通体的join
worker.stream().forEach(t -> {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Optional.of("All of capture work finished").ifPresent(System.out::println);
}
private static Thread createCaptureThread(String name) {
return new Thread(() -> {
Optional.of("The worker [" + Thread.currentThread().getName() + "] BEGIN capture data.")
.ifPresent(System.out::println);
synchronized (CONTROLS) {
while (CONTROLS.size() > MAX_WORKER) {//要是有5个就乖乖等着就可以
try {
CONTROLS.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
CONTROLS.addLast(new Control());//跳出while就说明可以跑了加入跑的队列 对公共数据操作要串行化保护的
}
// 这个是并行的
Optional.of("The worker [" + Thread.currentThread().getName() + "] is working...")
.ifPresent(System.out::println);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {//采集时间是10S 并行去执行任务
e.printStackTrace();
}
synchronized (CONTROLS) {//工作完了我吧自己移除工作队列
Optional.of("The worker [" + Thread.currentThread().getName() + "] END capture data.")
.ifPresent(System.out::println);
CONTROLS.removeFirst();// 为什么remove
// first?删掉的可能是自己也肯能是别人只要删掉就可以了 其实
CONTROLS.notifyAll();
}
}, name);
}
private static class Control {
}
}
对公共数据的操作要设置同步的保护的,这是真理。
------------------------------28----------控制并发-------------------------
自定义的锁解决synchronized的慢的问题:
代码chapter10
代码:
第一步:写一个接口
定义显示锁:
public interface Lock {
class TimeOutException extends Exception {
public TimeOutException(String message) {//报错信息
super(message);
}
}
void lock() throws InterruptedException;//加锁
void lock(long mills) throws InterruptedException, TimeOutException;//没有获取到锁就退出来了
void unlock();//释放锁
Collection<Thread> getBlockedThread();//获得被阻塞的线程
int getBlockedSize();//获得被阻塞的线程的个数
}
先看下最终的代码
package one.chapter10;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
public class BooleanLock implements Lock {
//The initValue is true indicated the lock have be get.
//The initValue is false indicated the lock is free (other thread can get this.)
private boolean initValue;
// 被阻塞的线程
private Collection<Thread> blockedThreadCollection = new ArrayList<>();
private Thread currentThread;//为什么第一这个变量,谁加的锁只能由谁去释放的。
public BooleanLock() {
this.initValue = false;
}
@Override
public synchronized void lock() throws InterruptedException {
while (initValue) {//true就是我不能抢到锁了就得等着
if(!blockedThreadCollection.contains(Thread.currentThread())){
blockedThreadCollection.add(Thread.currentThread());
}
this.wait();// 就在lock上wait
}
blockedThreadCollection.remove(Thread.currentThread());
this.initValue = true;
this.currentThread = Thread.currentThread();//设置lock是当前的线程虽然抢锁失败了
}
@Override
public synchronized void unlock() {//这个synchronized锁的是BooleanLock的实例
if (Thread.currentThread() == currentThread) {//是当前的线程才释放的
this.initValue = false;
Optional.of(Thread.currentThread().getName() + " release the lock monitor.")
.ifPresent(System.out::println);
this.notifyAll();
}
}
@Override
public synchronized void lock(long mills) throws InterruptedException, TimeOutException {
if (mills <= 0)
lock();
long hasRemaining = mills;//需要等的时间
long endTime = System.currentTimeMillis() + mills;//到什么时间结束
while (initValue) {//需要等待 醒了我就去抢锁,抢不到我就加时间 多次醒了请不到我就超时了 我直接抛出异常了
if (hasRemaining <= 0)
throw new TimeOutException("Time out");
if(!blockedThreadCollection.contains(Thread.currentThread())){
blockedThreadCollection.add(Thread.currentThread());
}
this.wait(mills);
hasRemaining = endTime - System.currentTimeMillis();
}
blockedThreadCollection.remove(Thread.currentThread());
this.initValue = true;
this.currentThread = Thread.currentThread();
}
/**
* 注意这个要考线程安全 其他的线程可以在其他的方法操作这个集合
* @return
*/
@Override
public Collection<Thread> getBlockedThread() {
return Collections.unmodifiableCollection(blockedThreadCollection);
}
@Override
public int getBlockedSize() {
return blockedThreadCollection.size();
}
}
----------29--------------------------------
第一个一个bug:
t1加的锁只能t1去释放,其他的是不能去释放的。
@Override
public synchronized void lock() throws InterruptedException {
while (initValue) {//true就是我不能抢到锁了就得等着
if(!blockedThreadCollection.contains(Thread.currentThread())){
blockedThreadCollection.add(Thread.currentThread());
}
this.wait();
}
blockedThreadCollection.remove(Thread.currentThread());
this.initValue = true;
this.currentThread = Thread.currentThread();//Thread.currentThread()这个是获取到锁的线程
}
@Override
public synchronized void unlock() {//这个synchronized锁的是BooleanLock的实例
if (Thread.currentThread() == currentThread) {//是当前的线程才释放的
this.initValue = false;
Optional.of(Thread.currentThread().getName() + " release the lock monitor.")
.ifPresent(System.out::println);
this.notifyAll();
}
}
第二个问题:synchronized不能打断,导致等待时间长,这个是synchronized的工作机制。
package chapter10;
public class SynchronizedProblem {
public static void main(String[] args) throws InterruptedException {
new Thread() {
@Override
public void run() {
SynchronizedProblem.run();
}
}.start();
Thread.sleep(1000);
Thread t2 = new Thread() {
@Override
public void run() {
// /sdfsdfsd
SynchronizedProblem.run();
//sdfsdfsd
System.out.println("继续做以下的事情t");
}
};
t2.start();
Thread.sleep(2000);
t2.interrupt();
System.out.println(t2.isInterrupted());
}
private synchronized static void run() {
System.out.println(Thread.currentThread());
while (true) {
}
}
}
t2可以打断但是不能中断进入
t2是一直block住的。我们不能让t2释放回来。做线程的其他的事情。
我们要解决这个问题。
@Override
public synchronized void lock(long mills) throws InterruptedException, TimeOutException {
if (mills <= 0)
lock();
long hasRemaining = mills;//需要等的时间
long endTime = System.currentTimeMillis() + mills;//到什么时间结束
while (initValue) {//需要等待 醒了我就去抢锁,抢不到我就加时间 多次醒了请不到我就超时了 我直接抛出异常了
if (hasRemaining <= 0)
throw new TimeOutException("Time out");
if(!blockedThreadCollection.contains(Thread.currentThread())){
blockedThreadCollection.add(Thread.currentThread());
}
this.wait(mills);
hasRemaining = endTime - System.currentTimeMillis();
}
blockedThreadCollection.remove(Thread.currentThread());
this.initValue = true;
this.currentThread = Thread.currentThread();
}
-------------------------------30-------------------------------