Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线程赢了';t等待后释放锁()_Java_Multithreading_Wait_Synchronized - Fatal编程技术网

Java 线程赢了';t等待后释放锁()

Java 线程赢了';t等待后释放锁(),java,multithreading,wait,synchronized,Java,Multithreading,Wait,Synchronized,很抱歉,如果还有其他类似的问题,但我找不到,我是并发编程的初学者,这个问题困扰了我一段时间,我真的需要了解我犯了什么错误,否则我无法继续我的作业 我试图实现的是从线程处理的“Test”中打印“1”,从同样由线程处理的“Test 2”中打印“from Test 2”,但只打印“1”。我该怎么办 ================================ import java.util.logging.Level; import java.util.logging.Logger; pub

很抱歉,如果还有其他类似的问题,但我找不到,我是并发编程的初学者,这个问题困扰了我一段时间,我真的需要了解我犯了什么错误,否则我无法继续我的作业

我试图实现的是从线程处理的“Test”中打印“1”,从同样由线程处理的“Test 2”中打印“from Test 2”,但只打印“1”。我该怎么办

================================

import java.util.logging.Level;
import java.util.logging.Logger;

public class Test implements Runnable {

    Third third;

    public Test(Third third){
        this.third = third;
    }

    public void run() {
        while (true) {
            synchronized (third) {
                try {
                    System.out.println("1");
                    third.wait();
                } catch (InterruptedException ex) {

                }

            }
        }
    }
}

=====================


public class Test2 implements Runnable{
    Test test;
    Third third;

    public Test2(Test test, Third third){
        this.test = test;
        this.third = third;
    }

    public void run(){
        while(true){
            synchronized(third){
                third.notifyAll();
                System.out.println("from test 2");
            }
        }
    }
}

================================

public class Third {

}

==============================

public class main {
    public static void main(String[] args) throws InterruptedException{

        Third third = new Third();
        Test test = new Test(third);
        Test2 test2 = new Test2(test, third);

        Thread t1 = new Thread(test);
        Thread t2= new Thread(test2);

        t1.run();
        t2.run();
        t2.join();
        t1.join();
    }
}

您不应该直接调用run方法,因为它不会创建任何线程。它像普通方法一样调用。您应该调用start()方法,而不是run()方法

还有一件事,你在用共享资源做什么。可能存在可由不同线程访问的同步方法

类名应以大写字母开头,并应遵循大小写

公共班机{ 公共静态void main(字符串[]args)引发InterruptedException{

    Third third = new Third();
    Test test = new Test(third);
    Test2 test2 = new Test2(test, third);

    Thread t1 = new Thread(test);
    Thread t2= new Thread(test2);

    t1.start();
    t2.start();
    t2.join();
    t1.join();
}

}

Ad@Gaurav正确地指出,您应该调用start()。代码中发生了什么:您有一个线程,它正在执行main(),并被困在监视器上等待回答,我在这里被困了几十个小时,不知道代码出了什么问题,甚至尝试了许多不同的方法,没有怀疑我在主课上的台词……我感觉很糟糕XD,但谢谢你!