0% found this document useful (0 votes)
88 views

One Java

The document contains source code for two Java multithreaded programs. The first program creates 10 threads that continuously print a number to the console. The second program is a multithreaded TCP server that accepts connections on port 8080 and responds to clients with the message "Hello client!".

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

One Java

The document contains source code for two Java multithreaded programs. The first program creates 10 threads that continuously print a number to the console. The second program is a multithreaded TCP server that accepts connections on port 8080 and responds to clients with the message "Hello client!".

Uploaded by

hj43us
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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