1.多个线程操作同一个数据时候
- 线程从哪里暂停就会从哪里开始
- 使用同步技术解决
- 多个线程一个锁,一个线程进入持有锁,别的线程就不能进入,执行完同步代码释放锁,别的线程才能持有锁
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ticket implements Runnable {
private static int tickets = 3;
private Object obj = new Object();不可以在run中创建
确保锁的共有性
public void run() {
synchronized (obj) {
while (tickets > 0) {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " "
+ tickets--);
} catch (Exception e) {
}
}
}
}
}
- 另一种synchronized 是将同步代码写在方法中,此时锁是this,如果是静态方法锁就是:本类.class
public static synchronized void tick() {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + " "
+ tickets--);
}
}
- 还有lock方法在jdk5之后
public class ticket implements Runnable {
private static int tickets = 3;
private Lock lock=new ReentrantLock();
需在成员变量位置声明确保不会因为线而产生多个锁
public void run() {
while (true) {
lock.lock();
if (tickets>0) {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()
+ " "+ tickets--);
} catch (Exception e) {
}finally
{
lock.unlock();
}
}
}
}
- 死锁
两个锁(a和b),两个线程(a和b)
每个线程两个同步代码块嵌套
a线程同步代码块锁a包含b
b线程b锁包含a锁
当出现两个线程分别拿到a和b锁,出现死锁
----------------------------------------
public class lockA {
确保锁唯一,b锁同样
public static final lockA locka=new lockA();
private lockA () {
}
}
-----------------------------
public class subRunable implements Runnable {
private int a=0;
@Override
public void run() {
while(true)
{
if (a%2==0) {a锁包含b锁
synchronized (lockA.locka) {
System.out.println("if----locka");
synchronized (lockB.lockb) {
System.out.println("if----lockb");
}
}
}else {b锁包含a锁
synchronized (lockB.lockb) {
System.out.println("elseif----lockb");
synchronized (lockA.locka) {
System.out.println("elseif----locka");
}
}
}
a++;换着顺序来
}
}
}
之后测试类中运行会出现a线程和b线程各拿一个锁
- wait()无限等待,notify()唤醒
- 多个线程需要同一个锁对象,可以在构造方法传入
- wait()notify()需要同一锁来调用,用在同步代码块中
- 共同的对象锁将多个线程联系起来,同步代码块用到锁,
wait notify用到锁让线程产生执行顺序