Java程序在调用线程#join()方法时冻结
我需要创建一个程序,以两种方式实现比较和递增两个计数器:未同步和已同步。所以我基本上有两个字段int counter&int counter2和一个run()方法,它只需迭代n次,比较计数器,增加计数器1,等待100毫秒,然后增加计数器2。 我有一个工作实现,但我需要使主线程最后完成,我有点困惑这样做。代码是:Java程序在调用线程#join()方法时冻结,java,Java,我需要创建一个程序,以两种方式实现比较和递增两个计数器:未同步和已同步。所以我基本上有两个字段int counter&int counter2和一个run()方法,它只需迭代n次,比较计数器,增加计数器1,等待100毫秒,然后增加计数器2。 我有一个工作实现,但我需要使主线程最后完成,我有点困惑这样做。代码是: public class Part3 { private int counter; private int counter2; public sync
public class Part3 {
private int counter;
private int counter2;
public synchronized boolean syncEquals() {
return counter == counter2;
}
public synchronized void syncIncrement() {
counter++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter2++;
}
public static void main(final String[] args) throws InterruptedException {
Part3 p = new Part3();
p.compare();
System.out.println("-----------------------");
p.compareSync();
}
public void compare() throws InterruptedException {
Thread t1 = new Thread(new Runnable(){
public void run(){
for(int i = 0; i < 10; i++) {
System.out.println((counter == counter2));
counter++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter2++;
}
}
});
Thread t2 = new Thread(new Runnable(){
public void run(){
for(int i = 0; i < 10; i++) {
System.out.println(counter == counter2);
counter++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter2++;
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
public synchronized void compareSync() throws InterruptedException {
counter = 0;
counter2 = 0;
Thread t1 = new Thread(new Runnable(){
public void run(){
for(int i = 0; i < 10; i++) {
System.out.println(syncEquals());
syncIncrement();
}
}
});
Thread t2 = new Thread(new Runnable(){
public void run(){
for(int i = 0; i < 10; i++) {
System.out.println(syncEquals());
syncIncrement();
}
}
});
t1.start();
t2.start();
}
}
公共类第三部分{
专用int计数器;
私人int计数器2;
公共同步布尔syncEquals(){
返回计数器==计数器2;
}
公共同步的void syncIncrement(){
计数器++;
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
计数器2++;
}
公共静态void main(最终字符串[]args)引发InterruptedException{
零件3 p=新零件3();
p、 比较();
System.out.println(“--------------------------”;
p、 比较同步();
}
public void compare()引发InterruptedException{
线程t1=新线程(新的可运行线程(){
公开募捐{
对于(int i=0;i<10;i++){
System.out.println((counter==counter2));
计数器++;
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
计数器2++;
}
}
});
线程t2=新线程(新可运行(){
公开募捐{
对于(int i=0;i<10;i++){
System.out.println(计数器==计数器2);
计数器++;
试一试{
睡眠(100);
}捕捉(中断异常e){
e、 printStackTrace();
}
计数器2++;
}
}
});
t1.start();
t2.start();
t1.join();
t2.连接();
}
public synchronized void compareSync()引发InterruptedException{
计数器=0;
计数器2=0;
线程t1=新线程(新的可运行线程(){
公开募捐{
对于(int i=0;i<10;i++){
System.out.println(syncEquals());
同步增量();
}
}
});
线程t2=新线程(新可运行(){
公开募捐{
对于(int i=0;i<10;i++){
System.out.println(syncEquals());
同步增量();
}
}
});
t1.start();
t2.start();
}
}
问题是当我在
compareSync()
中调用t1.join()
和t2.join()
时,程序冻结,甚至不打印此方法的任何结果。我是线程新手,想知道我做错了什么,所以提前谢谢你 你怎么知道它在连接时冻结了?哪个连接?问题在于同步的void compareSync()
方法。它与synchronized boolean syncEquals()
和synchronized void syncIncrement()
方法在同一对象上同步。如果调用t1.join();t2.连接()
在compareSync()
方法中,线程t1和t2永远无法进入syncEquals()
方法,也永远无法完成。要修复它,您需要从compareSync()
方法中删除synchronized
。我运行了它,但它没有冻结。它正常终止。版本:Neon.3发行版(4.6.3)构建id:20170314-1500