请教大侠。关于多线程同步问题

10-09-16 alvin841027
小弟写了一个程序。但是不知道怎么能做成singleton 同步多线程的模式。请各位大侠赐教,谢谢

package kid;

import java.io.BufferedReader;

import java.io.EOFException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.ServerSocket;

import java.net.Socket;

import java.net.SocketException;

import java.rmi.Naming;

import java.rmi.RemoteException;

import java.util.Calendar;

import entertainer.EntertainerIF;

import entertainer.KidId;

import entertainer.Message;

public class Kid {

private String kidname;

private String matchName;

private int localServerPort;

private ServerSocket servsock;

private EntertainerIF entertainer;

private Socket entertainersock;

private boolean gameover = false;

private KidId leftKid;

private KidId rightKid;

private Socket leftKidSock;

private Socket rightKidSock;

private boolean haveBaloon;

private Thread threadEntertainer;

private Thread threadLeftKid;

private Thread threadRightKid;

private Thread workingThread;

private int gameRound = 1;

public Kid() {

setThreadInfo();

}

private void setThreadInfo() {

// create reading socket thread

if (threadEntertainer == null) {

threadEntertainer = new Thread() {

@Override

public void run() {

boolean stop = false;

while (!gameover && !stop) {

try {

if (entertainersock != null) {

// System.out.println("Reading socket from entertainersock.....");

readMsg(entertainersock);

}

sleep(100);

//不能使用thread。sleep的方法

} catch (SocketException se) {

if (entertainersock.isClosed()) {

System.out

.println("entertainersock is closed.");

entertainersock = null;

stop = true;

}

} catch (EOFException ee) {

System.out

.println("Got EOFException, close entertainer socket.");

try {

entertainersock.close();

entertainersock = null;

stop = true;

} catch (Exception ex) {

猜你喜欢