threadPool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mk.ukim.finki.os.av6.threadPool; | |
import java.net.Socket; | |
import static mk.ukim.finki.os.av6.threadPool.TCPServer.workerPool; | |
public class ServerWorker extends Thread { | |
private Socket clientSocket; | |
private Integer clientID; | |
public ServerWorker(Socket clientSocket, Integer clientID) { | |
this.clientSocket = clientSocket; | |
this.clientID = clientID; | |
} | |
@Override | |
public void run() { | |
try { | |
execute(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(String.format("[WORKER %d] has finished", clientID)); | |
workerPool.release(); | |
} | |
private void execute() throws InterruptedException { | |
System.out.println(String.format("[WORKER %d] is working", clientID)); | |
sleep(10000); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mk.ukim.finki.os.av6.threadPool; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PrintWriter; | |
import java.net.InetAddress; | |
import java.net.Socket; | |
public class TCPClient extends Thread { | |
@Override | |
public void run() { | |
try { | |
startTCPClient(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void startTCPClient() throws IOException { | |
InetAddress address = InetAddress.getByName("localhost"); | |
Socket client = new Socket(address, 6868); | |
System.out.println("Connected to server"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mk.ukim.finki.os.av6.threadPool; | |
import java.util.HashSet; | |
public class TCPClientsSpawner { | |
private static int NUM_CLIENTS = 100; | |
public static void main(String[] args) throws InterruptedException { | |
System.out.println(String.format("Spawning %d clients", NUM_CLIENTS)); | |
HashSet<Thread> clients = new HashSet<>(); | |
for (int i = 0; i < NUM_CLIENTS; i++) { | |
clients.add(new TCPClient()); | |
} | |
for (Thread client : clients) { | |
client.start(); | |
} | |
for (Thread client : clients) { | |
client.join(1000); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mk.ukim.finki.os.av6.threadPool; | |
import mk.ukim.finki.os.lab3.DiningPhilosophersTest; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.concurrent.Semaphore; | |
public class TCPServer { | |
public static Semaphore workerPool; | |
private static Integer clientCounter; | |
private static void init() { | |
workerPool = new Semaphore(99); | |
clientCounter = 0; | |
} | |
public static void startTCPServer() throws IOException, InterruptedException { | |
ServerSocket serverSocket = new ServerSocket(6868); | |
while (true) { | |
System.out.println("Listening..."); | |
if (workerPool.tryAcquire()) { | |
Socket socket = serverSocket.accept(); | |
clientCounter++; | |
System.out.println("Got conncetion, assigning worker!"); | |
ServerWorker sw = new ServerWorker(socket, clientCounter); | |
sw.start(); | |
} else { | |
System.err.println("Server has reached the client limit, cannot accept new connections!"); | |
} | |
} | |
} | |
public static void main(String[] args) throws IOException, InterruptedException { | |
init(); | |
startTCPServer(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment