You are on page 1of 1

Archivo: /home/misan/Documents/java/One.

java Página 1 de 1

/* class One creates ten "printing" threads that print all to the console */
class One extends Thread {
String whatToPrint;
public One(String s) { whatToPrint = s; }

public static void main(String args[]) throws InterruptedException {


One th[] = new One[10];
for(int i=0;i<10;i++) {
new One(i+"").start();
}
}
public void run() {
while (true) {System.out.print(whatToPrint); try { sleep(10); } catch(InterruptedException e)
{} }
}

/* next is the source code of a multithreaded TCP server */

import java.net.*;
import java.io.*;
import java.util.Scanner;

class Server extends Thread {

Socket mySocket;

public Server(Socket s) {
mySocket = s;
}

public static void main(String args[]) throws IOException, InterruptedException{


ServerSocket ss = new ServerSocket(8080);

while(true) {
Socket s = ss.accept();
Server newServer = new Server(s);
newServer.start();
}

public void run() {


try {
PrintWriter output = new PrintWriter( mySocket.getOutputStream(), true);
while(true) {
output.println("Hello client!");
}

} catch(IOException e) {}

You might also like