serverwriter.java

来自「一个很好的JAVA聊天室系统 可以在课堂里实践并且进行聊天」· Java 代码 · 共 46 行

JAVA
46
字号
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import FIFO;

class ServerWriter extends Thread {

    protected chatserver server;
    public Vector OutputStreams;
    public FIFO outdata;
    private String outputline;

    public ServerWriter(chatserver s) { // constructor
	super(s.CurrentConnections, "Server Writer");
	server = s;
	OutputStreams = new Vector();
	outdata = new FIFO();
	this.start();  // start this thread
    }

    public synchronized void run() {  // lock this thread to prevent multiple access
	while(true) {
                try { this.wait();}      // wait for wakeup by notify()
		catch (InterruptedException e)
		     { System.out.println("Caught an Interrupted Exception");}
		
		outputline = outdata.pop(); // get the message from top of FIFO

		// lock the Vector to prevent the watcher thread from 
                // doing something with Vector before this code finishes

		synchronized(server.connections) {
			for (int i = 0; i < OutputStreams.size(); i++)
			    {   PrintStream out;
				out = (PrintStream) OutputStreams.elementAt(i);
				out.println(outputline);
			     }
		}    // end of synchronized	
	}  // end of while
    }

}
					
      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?