【问题标题】:run server in a thread在线程中运行服务器
【发布时间】:2018-12-23 10:24:13
【问题描述】:

我运行一次,布尔值“thisIsServer”为真,其他时间为假 但是方法“message.ChangeA();”不工作 (行 System.out.println("message modified"); 不起作用) 这是主要代码:

公共类主{ static Semaphore lock = new Semaphore(0);

public static boolean thisIsServer = false;
public static final int port = 8888;
public static Semaphore Lock = new Semaphore(0);
public static Semaphore Lock2 = new Semaphore(0);

public static Message message;
public static String command = new String();
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {


    if(thisIsServer){
        //This is Server
        Thread server = new Server();
        server.start();

        while (true){
            Lock.acquire();
            message.changeA();
            //Lock2.release();
            System.out.println("message modified");
        }
    }else{
        //This is Client
        Socket socket = new Socket("localhost", port);

        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());

        os.writeObject(new Message(10));
        while(true) {
            Message returnMessage = (Message) is.readObject();
            System.out.println("A is : " + returnMessage.a);
            os.writeObject(returnMessage);
        }
    }
}

}

服务器代码:

公共类服务器扩展线程{

public static final int port = 8888;

@Override
public void run() {
    //This is Server
    ServerSocket serverSocket = null;
    try {
        serverSocket = new ServerSocket(port);

        Socket socket = serverSocket.accept();

        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());



        while (true) {
            Main.message = (Message) is.readObject();
            System.out.println("message received");
            Main.lock.release();
            synchronized (this){
                wait(1000);
            }

            System.out.println("message sent");
            os.writeObject(Main.message);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

}

消息代码:

公共类Message实现Serializable{

public Message(int a){
    this.a = a;
}

public int a;
public void changeA(){
    a = 2*a;
}

}

【问题讨论】:

  • 只是好奇,但您为什么要将帖子恢复为格式较少的版本?
  • 只是更喜欢它:)
  • 嗯,好的,这很好,但其他人可能更喜欢格式化的方式,因为它更容易阅读。但是,如果您更喜欢当前的方式,那是您的帖子,您的选择。干杯。

标签: java multithreading server client


【解决方案1】:

我认为这是因为您将 Lock 定义为“Lock”和“lock”两种形状。 但是您已经使用 Lock.acquire() 等待,并且在线程中解锁时使用了 Main.lock.release()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多