请问 如何让2个类中的synchronized共享同一个锁(ex21_1的对象的引用)类 ex21_1 一开始先wait(); 然后等待ex21_2执行notifyAll();后开始println;但是 我没有能够实现 卡住了 我想要ex21_1 和 ex21_2中的 synchronized run();共用一把锁--ex21_1的对象的引用。请问如何实现或者修改 谢谢。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ex21 { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
ex21_1 a = new ex21_1();
ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(a);
exec.execute(new ex21_2(a));
}}class ex21_1 implements Runnable{
public synchronized void run() {
try{wait();} catch (InterruptedException ex){}
System.out.println("1 is running");
}
}class ex21_2 implements Runnable{
private ex21_1 a;
public ex21_2(ex21_1 a){this.a = a;}
public synchronized void run(){
if(a instanceof ex21_1){
try{TimeUnit.MICROSECONDS.sleep(1);}
catch (InterruptedException ex){}
notifyAll();
}

}

}

解决方案 »

  1.   


    public static Object locker=new Object();synchronized(locker)
    {
    //your code

      

  2.   

    共用一把锁 -- ex21_1的对象的引用。 还有一定要用synchronized代码块实现吗? synchronized方法 我想用
      

  3.   

    既然 你讲到了 共用一把锁
    那么。你只要能够确定 synchronized锁的对象是同一个就可以。既然你用的了2个类来实现共享
    那么能共享同一把锁的事情,不外乎就是传值共享。所以public void run()
    {
    synchronized(x)
    {
    ..........
    }
    }
    这种方式比较适合
      

  4.   

    题目是这样的: 创建2个Runnable,其中一个的run()方法启动并调用wait(),而第二个类应该捕获第一个Runnable对象的引用,其run()方法应该在一定的秒数之后,为第一个任务调用notifyAll(), 从而使得第一个任务可以显示一条信息。使用Executor来测试你的类。 from thinkin java.
      

  5.   

    package com.xh.dao;public class ThreadTest {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    final Operation o = new Operation();
    new Thread(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    o.write();
    }
    }).start();
    new Thread(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    o.read();
    }
    }).start();
    }
    }
    class Operation {
    private boolean target = true;
    public synchronized void write() {
    try {
    if (target) {
    wait();
    }
    System.out.println("write()");
    target = !target;
    notify();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    public synchronized void read() {
    try {
    if (!target) {
    wait();
    }
    System.out.println("write()");
    target = !target;
    notify();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    }
      

  6.   

    直接复制的,刚刚有点问题
    package com.xh.dao;public class ThreadTest {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    final Operation o = new Operation();
    new Thread(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    o.write();
    }
    }).start();
    new Thread(new Runnable(){
    public void run() {
    // TODO Auto-generated method stub
    o.read();
    }
    }).start();
    }
    }
    class Operation {
    private boolean target = true;
    public synchronized void write() {
    try {
    while (true) {
    if (target) {
    wait();
    }
    System.out.println("write()");
    target = !target;
    notify();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    public synchronized void read() {
    try {
    while (true) {
    if (!target) {
    wait();
    }
    System.out.println("read()");
    target = !target;
    notify();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    }
      

  7.   

    a作为全局变量保存第一个runnable对象的引用import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    public class ex21 {
        ex21_1 a;
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            a = new ex21_1();
            ExecutorService exec = Executors.newCachedThreadPool();
            exec.execute(a);
            exec.execute(new ex21_2(a));
        }}class ex21_1 implements Runnable{
        public void run() {
            synchronized(this){
            try{wait();} catch (InterruptedException ex){}
            System.out.println("1 is running");
            }
        }
    }class ex21_2 implements Runnable{
        private ex21_1 a;
        public ex21_2(ex21_1 a){this.a = a;}
        public void run(){
            synchronized(a){
            if(a instanceof ex21_1){
              a.notifyAll();
            }
            }
        }
        
    }
      

  8.   

    这是我写的测试代码,不知道是否和你的问题差不多。class Knowing{
    T1 t1;
    public static void main(String[] args){
    Knowing k=new Knowing();
    k.start();
     
    }
    public void start(){
    t1=new T1();
     T2 t2=new T2();
     new Thread(t1).start();
     new Thread(t2).start();
    }

    class T1 implements Runnable{ @Override
    public void run() {
    synchronized (this){
    try {
    wait();
    System.out.println("Hello World!!");

    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }
    class T2 implements Runnable{ @Override
    public void run() {
    synchronized(t1){
    t1.notifyAll();
    }
    }

    }


    }
      

  9.   

    在调用wait或者notifyAll方法的时候应该注意此线程必须是该对象监视器的所有者。
    当前线程变为对象监视器的所有者有3个方法:
    1,通过执行此对象的同步实例方法。 
    2,通过执行在此对象上进行同步的 synchronized 语句的正文。 
    3,对于 Class 类型的对象,可以通过执行该类的同步静态方法。