java中Synchronized引起的并发线程的无限等待的解决方法

Synchronized引起的并发线程的无限等待的解决方法 

我们在数据库并发访问中经常用到:select * from table for update,这句话会引起所有执行这句话的线程排队,一个一个的序列执行。等待的线程只能死等,直到超时为止。下面程序的f1就模仿这句话的感觉。



例1.9.6:

class A {
    public synchronized void f1() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("f1模仿select * from table for update,执行的很慢"+Thread.currentThread().getName());
    }
}

class MyThread1 extends Thread {
    A a;
    public MyThread1(A a) {
        this.a = a;
    }
    public void run() {
        a.f1();
    }
}


public class TestMark_to_win {
    public static void main(String[] args) {
        MyThread1[] threads = new MyThread1[3];
        A a = new A();
        for (int i = 0; i < 3; i++) {
            threads[i] = new MyThread1(a);
            threads[i].start();
        }
    }
}

输出结果:
f1模仿select * from table for update,执行的很慢Thread-0
f1模仿select * from table for update,执行的很慢Thread-2
f1模仿select * from table for update,执行的很慢Thread-1


下面程序的concuNum会记录,现在队列里多少人在排队。selectForUpdateIntel就成了智能版的select * from table for update,人少我就排队,人多我就直接撤。





例1.9.6:

class A {
    int concuNum=0;
    private synchronized void selectForUpdateSyn() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("模仿select * from table for update,执行的很慢"
                + Thread.currentThread().getName()+"目前队中有"+concuNum+"人");
    }
  
    public void selectForUpdate() {
        concuNum++;
        selectForUpdateSyn();
        concuNum--;
    }
    public void selectForUpdateIntel() {
        if(concuNum<3){
            System.out.println("我是聪明人,目前队中人不多,有"+concuNum+"人,我等");
            selectForUpdate();
        }else {
            System.out.println(concuNum+"! 我是聪明人,排队人太多,我先不排了");
        }
    }
}

class MyThread1 extends Thread {
    A a;

    public MyThread1(A a) {
        this.a = a;
    }

    public void run() {
        a.selectForUpdate();
    }
}


class MyThread2 extends Thread {
    A a;
    public MyThread2(A a) {
        this.a = a;
    }
    public void run() {
        a.selectForUpdateIntel();
    }
}

public class TestMark_to_win {
    public static void main(String[] args) {
        MyThread1[] threads = new MyThread1[3];
        A a = new A();
        for (int i = 0; i < 3; i++) {
            threads[i] = new MyThread1(a);
        }
        MyThread2 myThread2=new MyThread2(a);

        threads[0].start();
        threads[1].start();
        myThread2.start();
        threads[2].start();
    }
}

运行结果:
3! 我是聪明人,排队人太多,我先不排了
模仿select * from table for update,执行的很慢Thread-0目前队中有3人
模仿select * from table for update,执行的很慢Thread-1目前队中有2人
模仿select * from table for update,执行的很慢Thread-2目前队中有1人

运行结果有时又变成:

我是聪明人,目前队中人不多,有1人,我等
模仿select * from table for update,执行的很慢Thread-1目前队中有4人
模仿select * from table for update,执行的很慢Thread-2目前队中有3人
模仿select * from table for update,执行的很慢Thread-0目前队中有2人
模仿select * from table for update,执行的很慢Thread-3目前队中有1人