相关疑难解决方法(0)

我为什么不#include <bits/stdc ++.h>?

我在我的代码中发布了一个问题,其唯一的#include指令如下:

#include <bits/stdc++.h>
Run Code Online (Sandbox Code Playgroud)

我的老师告诉我这样做,但在评论部分,我被告知我不应该这样做.

为什么?

c++ portability c++-faq turbo-c++ implementation-defined-behavior

215
推荐指数
6
解决办法
2万
查看次数

使用线程进行奇数偶数打印

奇数偶数打印使用thread.Create一个线程类,两个线程实例.一个将打印奇数,另一个将打印偶数.

我做了以下编码.但它涉及死锁状态.有人可以解释一下可能是什么原因吗?

public class NumberPrinter implements Runnable{
private String type;
private static boolean oddTurn=true;


public NumberPrinter(String type){
    this.type=type;
}
public void run() {
    int i=type.equals("odd")?1:2;
    while(i<10){
        if(type.equals("odd"))
            printOdd(i);
        if(type.equals("even"))
            printEven(i);
        i=i+2;
    }

}

private synchronized void printOdd(int i){
    while(!oddTurn){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.out.println(type + i);
    oddTurn=false;
    notifyAll();
}

private synchronized  void printEven(int i){
    while(oddTurn){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace(); 
        }
    }
    System.out.println(type + i);
    oddTurn=true;
    notifyAll();

}

public …
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-safety

7
推荐指数
1
解决办法
1万
查看次数