如何使该客户端成为多线程客户端?
我已经阅读了很多有关多线程客户端的内容,但是对于这个,我无法使其成为多线程! 你能帮我吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的回答:是的。每次调用方法 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.
多线程是指使用多个线程。从技术上讲,是的,你是多线程的。
简短的回答,是的。
答案很长,您需要确保您的方法是线程安全的,并且 MainCient 不会阻塞或被任何其他线程阻塞。
当多线程处理时,我通常倾向于创建一个集中类来控制线程“池”。我称之为调度员。它管理线程映射。
Multithreading is the use of more than one thread. Technically yes you are multithreading.
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.