example5.java

来自「java多线程编程的例子」· Java 代码 · 共 36 行

JAVA
36
字号
package socketthread;import java.net.*;import java.io.*;class Example5 extends Thread {  volatile boolean stop = false;  volatile ServerSocket socket;  public static void main( String args[] ) throws Exception {    Example5 thread = new Example5();   System.out.println( "Starting thread..." );   thread.start();   Thread.sleep( 3000 );   System.out.println( "Asking thread to stop..." );   thread.stop = true;   //thread.socket.close();   Thread.sleep( 3000 );   System.out.println( "Stopping application..." );   //System.exit( 0 );  }  public void run() {    try {      socket = new ServerSocket(7856);    } catch ( IOException e ) {     System.out.println( "Could not create the socket..." );      return;    }    while ( !stop ) {     System.out.println( "Waiting for connection..." );      try {       Socket sock = socket.accept();      } catch ( IOException e ) {      System.out.println( "accept() failed or interrupted..." );      }    }   System.out.println( "Thread exiting under request..." );  }}

⌨️ 快捷键说明

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