如何使该客户端成为多线程客户端?

发布于 2024-08-17 19:53:47 字数 2501 浏览 1 评论 0原文

我已经阅读了很多有关多线程客户端的内容,但是对于这个,我无法使其成为多线程! 你能帮我吗?

public class MainClient implements Runnable{

private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;

public static String getText() {
    return text;
}

public static void setText(String text) {
    MainClient.text = text;
}
private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {



    MainFrame farme = new MainFrame();
    farme.setVisible(true);
    try {
        c = new Socket("localhost", 5050);


        os = new PrintWriter(c.getOutputStream(), true);


        is = new BufferedReader(new InputStreamReader(c.getInputStream()));
    } catch (UnknownHostException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void active() {

    String teXt = MainClient.getText();
    System.out.println(teXt);
    os.println(teXt);

    try {
        String line = is.readLine();
        System.out.println("Text received: " + line);
        os.flush();
        is.close();
        is.close();
        c.close();
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }

      }
}

当客户端在文本区域写入内容然后单击发送按钮时,也会调用 active 方法。

2)我还有一个问题:

在另一个类中,我为我的发送按钮执行了此操作,这是否意味着客户端是多线程的?

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new Thread(new Runnable() {

        @Override
        public void run() {
            // This gets run in a background thread
            String text = jTextArea1.getText();


            jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
            MainClient.setText(client.getCurrentName() + " : " + text + "\n");
            clear();
            MainClient.active();

        }
    }).start();




}

最后编辑:

这是我的活动方法:

  public static void active() {

    String teXt = MainClient.getText();

    os.println(teXt);


        String line = is.readLine();
        System.out.println("Text received: " + line);
        os.flush();
        is.close();
        is.close();
        c.close();
    }

I have read a lot about multithread client but for this one,I can not make it multithread!
would you please help me?

public class MainClient implements Runnable{

private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;

public static String getText() {
    return text;
}

public static void setText(String text) {
    MainClient.text = text;
}
private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {



    MainFrame farme = new MainFrame();
    farme.setVisible(true);
    try {
        c = new Socket("localhost", 5050);


        os = new PrintWriter(c.getOutputStream(), true);


        is = new BufferedReader(new InputStreamReader(c.getInputStream()));
    } catch (UnknownHostException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void active() {

    String teXt = MainClient.getText();
    System.out.println(teXt);
    os.println(teXt);

    try {
        String line = is.readLine();
        System.out.println("Text received: " + line);
        os.flush();
        is.close();
        is.close();
        c.close();
    } catch (IOException ex) {
        Logger.getLogger(MainClient.class.getName()).log(Level.SEVERE, null, ex);
    }

      }
}

also active method will be called when the client write something on the text area and then clicks on the send button.

2) also i have a question that:

in the other class I have this action performed for my send button,does it mean that client is multithread??

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new Thread(new Runnable() {

        @Override
        public void run() {
            // This gets run in a background thread
            String text = jTextArea1.getText();


            jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
            MainClient.setText(client.getCurrentName() + " : " + text + "\n");
            clear();
            MainClient.active();

        }
    }).start();




}

Last EDIT:

this is my active method:

  public static void active() {

    String teXt = MainClient.getText();

    os.println(teXt);


        String line = is.readLine();
        System.out.println("Text received: " + line);
        os.flush();
        is.close();
        is.close();
        c.close();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-08-24 19:53:47

简短的回答:是的。每次调用方法 jButton1ActionPerformed 时,第二个片段中的代码都会创建一个新线程。

我不确定这是否是预期的行为。

short answer: YES. The code in the second snippet would create a new thread everytime the method jButton1ActionPerformed is called.

I am not sure if this is the intended behavior though.

不醒的梦 2024-08-24 19:53:47

这是否意味着客户端是多线程的?

多线程是指使用多个线程。从技术上讲,是的,你是多线程的。

那么,我可以同时运行 2 个客户端吗?

简短的回答,是的。
答案很长,您需要确保您的方法是线程安全的,并且 MainCient 不会阻塞或被任何其他线程阻塞。

当多线程处理时,我通常倾向于创建一个集中类来控制线程“池”。我称之为调度员。它管理线程映射。

does it mean that client is multithread??

Multithreading is the use of more than one thread. Technically yes you are multithreading.

So ,can I run 2 clients at the same time?

Short answer, Yes.
Long answer, you will need to make sure your methods are thread-safe and the MainCient is not blocking or being blocked by any other thread.

When Multithreading, i usually tend to make a centralized class to control the "pool" of Threads. I call it the Dispatcher. Which manages a map of threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文