使用线程锁定系统的偶数奇数

时间:2018-07-04 17:10:51

标签: java multithreading core

今天,我在Thread上进行了一些练习,并试图创建一个偶数奇数程序。我使用同步创建了它,并且运行良好。 但是当我尝试使用Lock进行相同的操作时,我就卡住了。

下面是我尝试执行的代码。

public class OddEvenNumberThreadLock {

public static void main(String args[]) {

    SharedObject sharedObject = new SharedObject();

    Thread evenThread = new Thread(new EvenNumber(sharedObject));
    Thread oddThread = new Thread(new OddNumber(sharedObject));

    evenThread.start();
    oddThread.start();
}

}



 class EvenNumber implements Runnable {
SharedObject object;

public EvenNumber(SharedObject object) {
    this.object = object;
}

@Override
public void run() {

    for (int i = 0; i <= 100; i = i + 2) {

        while (!object.isOdd()) {
            object.getLock().lock();
            try {
                System.out.println("Even : " + i);
                object.setOdd(true);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                object.getLock().unlock();

            }

        }

    }

}

}
class OddNumber implements Runnable {
SharedObject object;

public OddNumber(SharedObject object) {
    this.object = object;
}

@Override
public void run() {

    for (int i = 1; i <= 100; i = i + 2) {

        while (object.isOdd()) {
            object.getLock().lock();
            try {
                System.out.println("Odd : " + i);
                object.setOdd(false);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                object.getLock().unlock();

            }

        }

    }

}
}

class SharedObject {
private Lock lock;
private boolean isOdd;

public SharedObject() {
    this.lock = new ReentrantLock();

}

public boolean isOdd() {
    return isOdd;
}

public void setOdd(boolean isOdd) {
    this.isOdd = isOdd;
}

public Lock getLock() {
    return lock;
}

public void setLock(Lock lock) {
    this.lock = lock;
}
}

我还有一个问题,例如在同步的情况下,我们使用notify方法通知其他线程。在Lock的情况下,我们如何实现这一目标。

谢谢

1 个答案:

答案 0 :(得分:1)

据我所知,您要实现自己的两个线程以乒乓方式打印偶数和奇数。使用ReentrantLock比使用synchronized块更容易实现所需的行为,因为synchronized总是不公平的,但是可以使用适当的构造函数使ReentrantLock公平。这是Lock s程序的外观:

public class App {

    public static void main(String args[]) {

        SharedObject sharedObject = new SharedObject();

        Thread evenThread = new Thread(new EvenNumber(sharedObject));
        Thread oddThread = new Thread(new OddNumber(sharedObject));

        evenThread.start();
        oddThread.start();
    }

}


class EvenNumber implements Runnable {
    SharedObject object;

    public EvenNumber(SharedObject object) {
        this.object = object;
    }

    public void run() {
        int i = 0;
        while(i <= 100) {
            object.getLock().lock();
            try {
                if (!object.isOdd()) {
                    System.out.println("Even : " + i);
                    i = i + 2;
                    object.setOdd(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                object.getLock().unlock();

            }
        }
    }
}

class OddNumber implements Runnable {
    SharedObject object;

    public OddNumber(SharedObject object) {
        this.object = object;
    }

    public void run() {
        int i = 1;
        while(i <= 100) {

            object.getLock().lock();
            try {
                if(object.isOdd()) {
                    System.out.println("Odd : " + i);
                    i = i + 2;
                    object.setOdd(false);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                object.getLock().unlock();

            }

        }

    }
}

class SharedObject {
    private Lock lock;
    private boolean isOdd;

    public SharedObject() {
        this.lock = new ReentrantLock(true);

    }

    public boolean isOdd() {
        return isOdd;
    }

    public void setOdd(boolean isOdd) {
        this.isOdd = isOdd;
    }

    public Lock getLock() {
        return lock;
    }

    public void setLock(Lock lock) {
        this.lock = lock;
    }

}