版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34846877/article/details/81981625
1.多线程死锁
1.1什么是多线程死锁?
同步中嵌套同步,导致锁无法释放
代码示例:
class Thread009 implements Runnable {
private int trainCount = 100;
private Object oj = new Object();
public boolean flag = true;
public void run() {
if (flag) {
while (trainCount > 0) {
synchronized (oj) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
sale();
}
}
} else {
while (trainCount > 0) {
sale();
}
}
}
public synchronized void sale() {
synchronized (oj) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
if (trainCount > 0) {
System.out.println(Thread.currentThread().getName() + "," + "出售第" + (100 - trainCount + 1) + "票");
trainCount--;
}
}
}
}
public class Test009 {
public static void main(String[] args) throws InterruptedException {
Thread009 threadTrain = new Thread009();
Thread t1 = new Thread(threadTrain, "窗口1");
Thread t2 = new Thread(threadTrain, "窗口2");
t1.start();
Thread.sleep(40);
threadTrain.flag = false;
t2.start();
}
}
2.ThreadLocal
2.1什么是ThreadLocal?
ThreadLocal提高一个线程的局部变量,访问某个线程拥有自己局部变量。
当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本。,
2.1.1ThreadLocal的接口方法
ThreadLocal类接口很简单,只有4个方法。
1. void set(object values) 设置当前线程的线程局部变量的值。
2. public Object get() 该方法返回当前线程锁对应的线程局部变量
3. public void remove() 将当前线程局部标量的值删除,目的是为了减少内存的占用,该方法是
JDK5.0 新增的方法。需要指出的是当线程结束后,对应该线程的剧本变量将自动被垃圾回收,
所以显示调用该方法清除线程的局部变量并不是必须的操作,但它可以加快内存回收的速度。
4. protected Object initalValue() 返回该线程局部变量的初始值,该方法是一个protected 的方法,显然是为了
让子类覆盖而设计的,这个方法是一个延迟调用的方法,在线程第一次调用get()或 set(Object)时才执行。
并且仅执行一次。ThreadLocal中的缺省实现直接返回一个null
代码示例:创建三个线程,每个线程生成自己独立的序列号。
class Res {
// 生成序列号共享变量
public static Integer count = 0;
public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return 0;
};
};
public Integer getNum() {
int count = threadLocal.get() + 1;
threadLocal.set(count);
return count;
}
}
public class ThreadLocalTest extends Thread {
private Res res;
public ThreadLocalTest(Res res) {
this.res = res;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + "---" + "i---" + i + "--num:" + res.getNum());
}
}
public static void main(String[] args) {
Res res = new Res();
ThreadLocalTest threadLocaDemo1 = new ThreadLocalTest(res);
ThreadLocalTest ThreadLocalTest = new ThreadLocalTest(res);
ThreadLocalTest threadLocaDemo3 = new ThreadLocalTest(res);
threadLocaDemo1.start();
ThreadLocalTest.start();
threadLocaDemo3.start();
}
}
2.1.2ThreadLocal实现原理
ThreadLocal通过map集合
Map.put(“当前线程”,value)