首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java socket可以线程同步吗,该怎么处理

2012-12-14 
java socket可以线程同步吗import java.awt.*import java.awt.event.*import java.io.DataInputStreami

java socket可以线程同步吗


import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class ZhuFrame extends JFrame implements ActionListener{
JPanel title=new JPanel();
JLabel head=new JLabel();
JLabel user=new JLabel();
DefaultMutableTreeNode node1=new  DefaultMutableTreeNode("我的好友"); // 定义树结点
DefaultMutableTreeNode node2=new  DefaultMutableTreeNode("我  的  群"); // 定义树结点
DefaultMutableTreeNode node3=new  DefaultMutableTreeNode("在线用户"); // 定义树结点
JTextField searchjtf=new JTextField(15);
JButton searchjbt=new JButton("查  找");
JTree friendsjtr=new JTree(node1);
JTree myqun=new JTree(node2);
JTree searchonlinejtr=new JTree(node3);
JPanel showjpa=new JPanel(new BorderLayout());
JPanel showjpa1=new JPanel(new BorderLayout());
JPanel showjpa2=new JPanel(new BorderLayout());
Socket socket;
DataInputStream input;
DataInputStream din;
DataOutputStream output;
DataOutputStream dout;
String username;//用户学号
String name;//聊天对象的学号
String ip="";//聊天对象的IP
String port="";//聊天对象的端口
String localport="";
boolean pass=false;
Thread th;
private static Lock lock=new ReentrantLock();
public ZhuFrame(String userName,Socket socket){
Dimension screen=getToolkit().getScreenSize();
this.username=userName;
this.socket=socket;
this.setSize(280, 530);
this.setTitle(username);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocation(screen.width/7*5,100);
this.setVisible(true);

th=new Thread(new SocketHandler(socket));
th.start();

try{
input=new DataInputStream(socket.getInputStream());
output=new DataOutputStream(socket.getOutputStream());
output.writeUTF("getfriends "+username);
String friends=input.readUTF();
String[] fri=friends.split("#");
for(String i:fri){
node1.add(new  DefaultMutableTreeNode(i));
}
output.writeUTF("getqun "+username);
String qun=input.readUTF();
String[] quna=qun.split("#");
for(String i:quna){
node2.add(new  DefaultMutableTreeNode(i));
}
output.writeUTF("searchonline");
String[] online=input.readUTF().split("[ ]");
int j=0;
for(int i=0;i<online.length/4;i++){
node3.add(new DefaultMutableTreeNode(online[j]));
j+=4;
}
}catch(Exception e){
e.printStackTrace();
}

myqun.setRootVisible(true);
friendsjtr.setRootVisible(true);
searchonlinejtr.setRootVisible(true);
title.add(head);
title.add(user);
title.add(searchjtf);
title.add(searchjbt);
showjpa.add(friendsjtr,BorderLayout.NORTH);
showjpa.add(showjpa1,BorderLayout.CENTER);
showjpa1.add(myqun,BorderLayout.NORTH);
showjpa1.add(showjpa2,BorderLayout.CENTER);
showjpa2.add(searchonlinejtr,BorderLayout.NORTH);
showjpa2.setBackground(Color.WHITE);
showjpa.setBackground(Color.WHITE);


this.add(title,BorderLayout.NORTH);
this.add(showjpa,BorderLayout.CENTER);
searchjbt.addActionListener(this);

}
private static class SocketHandler implements Runnable{
DataOutputStream out;
DataInputStream in;
private Socket sock;
public SocketHandler(Socket socket){
this.sock=socket;
}
public void run(){
try{
out=new DataOutputStream(sock.getOutputStream());
in=new DataInputStream(sock.getInputStream());
String[] str=in.readUTF().split("[ ]");
while(true){
if(str[0]!=null){
 int n = JOptionPane.showConfirmDialog(null, "您有消息!", "消息框", JOptionPane.YES_NO_OPTION); 
         if (n == JOptionPane.YES_OPTION) { 
         ChatFrame f=new ChatFrame("1111",sock,str[1],str[2],str[3]);
         f.setVisible(true);
         } else if (n == JOptionPane.NO_OPTION) { 
             return ;
         } 
}
else{
break;
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e){
try{
pass=true;
if(e.getSource()==searchjbt){
if(!searchjtf.getText().equals("")){
output.writeUTF("search "+searchjtf.getText());
String string;
if((string=input.readUTF()).equals("no")){
JOptionPane.showMessageDialog(this, "用户不在线!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return ;
}
else{
String[] m=string.split("[ ]");
ip=m[0];
port=m[1];
name=m[2];
localport=m[3];

new Thread(new con(username,socket,ip,port,localport)).start(); 
this.setVisible(true);
JOptionPane.showMessageDialog(this, "查找成功!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return ;
}
}
else{
JOptionPane.showMessageDialog(this, "请输入学号!", "系统提示", JOptionPane.INFORMATION_MESSAGE);
return ;
}
}
/*if(e.getSource()==searchonlinejbt){
output.writeUTF("searchonline");
String[] online=input.readUTF().split("[ ]");
int j=0;
showjpa.removeAll();
for(int i=0;i<online.length/4;i++){
JLabel jl=new JLabel(online[j]);
jl.setHorizontalAlignment(JLabel.CENTER);
LineBorder border=new LineBorder(Color.yellow);
jl.setBorder(border);
showjpa.add(jl);
j+=4;
}
this.setVisible(true);
}*/
}catch(Exception ex){
ex.printStackTrace();
}
}
class con implements Runnable{
String usernamea="";
Socket socketa=null;
String ipa="";
String porta="";
String localporta="";
public con(String a,Socket b,String c,String d,String e){
this.usernamea=a;
this.socketa=b;
this.ipa=c;
this.porta=d;
this.localporta=e;
}
public void run(){
ChatFrame f=new ChatFrame(usernamea,socketa,ipa,porta,localporta);
f.setVisible(true);
}


}
}



这样写有错,但是如果把th=new Thread(new SocketHandler(socket));  th.start();写在myqun.setRootVisible(true);这一句前面就不会有错,我想是有2个线程在用socket的生成的流,可以怎么让socket线程同步或是让那个流同步。


[最优解释]
最好把负责读写该Socket的方法用一个类统一进行管理,然后读写方法前面加上synchronized进行同步
[其他解释]
哦,我试试。。。。

热点排行