线程在调试模式下运行,但不能实时工作

时间:2016-08-11 08:48:31

标签: java multithreading

我编写的服务器应用程序,适用于少数线程。我们有连接和交换数据的网络线程,客户端应用程序,dataBaseConnector线程,连接sqlite数据库,userInterface线程,用于简单控制程序。

主:

public class Main {

public static void main(String[] args) throws Exception {
    ServerSocket serverSocket = new ServerSocket(9000);
    DataContainer dataContainer = new DataContainer();
    DataBaseConnector dataBaseConnector = new DataBaseConnector(dataContainer);
    UserInterface userInterface = new UserInterface(dataBaseConnector, dataContainer);
    Thread userInterfaceThread = new Thread(userInterface);
    Thread dataBaseConnectorThread = new Thread(dataBaseConnector);
    userInterfaceThread.start();
    dataBaseConnectorThread.start();
    while(dataContainer.workingStatus){
        new Thread(new Handler(new Networking(dataContainer, serverSocket.accept()))).start();
    }
}
}

用户界面:

public class UserInterface implements Runnable {

private final String WELCOME = "Witaj w programie Auction House! Jest to serwer aplikacji";
private final String MENU = "Menu:";
private final String OPTION_ONE = "1. Zapisz dane do bazy";
private final String OPTION_TWO = "2. Zamknij serwer";
private final String OPTION_THREE = "3. Przywroc menu";
private DataBaseConnector dataBaseConnector;
private DataContainer dataContainer;
private int userChoice = 0;

public UserInterface(DataBaseConnector dataBaseConnector, DataContainer dataContainer) {
    this.dataBaseConnector = dataBaseConnector;
    this.dataContainer = dataContainer;
    setUpMenu();
}

@Override
public void run() {
    while (dataContainer.workingStatus){
        getUserChoice();
        makeUserRequest();
    }
}

private void setUpMenu() {
    System.out.println(WELCOME);
    System.out.println(MENU);
    System.out.println(OPTION_ONE);
    System.out.println(OPTION_TWO);
    System.out.println(OPTION_THREE);
}

private void getUserChoice() {
    Scanner scanner = new Scanner(System.in);
    userChoice = scanner.nextInt();
}

private void makeUserRequest() {
    switch (userChoice) {
    case 1: {
        dataContainer.lockTheLock();
        dataContainer.checkPoint = true;
        dataContainer.unlockTheLock();
        break;
    }
    case 2: {
        dataContainer.lockTheLock();
        dataContainer.workingStatus = false;
        dataContainer.unlockTheLock();
        break;
    }
    case 3: {
        setUpMenu();
        break;
    }
    }
}
}

1 - 用于将数据写入数据库 2 - 用于关闭服务器 3 - 再次打印菜单

和DataBaseConnector(简短版):

public DataBaseConnector(DataContainer dataContainer) {
    getJDBCDriverClass();
    connection = connectToDataBase();
    statement = createStatement();
    this.dataContainer = dataContainer;
    createTables();
    selectDataFromDataBase();
    updateDataContainer();
    clearLists();
}

@Override
public void run() {
    while (dataContainer.workingStatus) {
        if (dataContainer.checkPoint) {
            dataContainer.lockTheLock();
            dropTables();
            createTables();
            setClientList();
            setAuctionList();
            setItemList();
            instertDataIntoDataBase();
            dataContainer.checkPoint = false;
            dataContainer.unlockTheLock();
        }
    }
}

锁定:

public class Lock {
private boolean isLocked = false;

public synchronized void lock() throws InterruptedException {
    while (isLocked) {
        wait();
    }
    isLocked = true;
}

public synchronized void unlock() {
    isLocked = false;
    notifyAll();
    try {
        wait(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

问题:当在eclipse中以调试模式运行时,一切正常,但实时当我在控制台中选择1时,没有任何事情发生,但是当选择3时,菜单再次打印。

1 个答案:

答案 0 :(得分:0)

当您在Eclipse中工作时,该程序在您的计算机上,但是当您在网络上工作时,网络防火墙可能存在问题。检查使用套接字的9000端口是否在防火墙上打开,当然只能在您使用它的网络中打开。