Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task threads #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions L7_threads/src/main/java/ru/track/prefork/Client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ru.track.prefork;

import java.io.*;
import java.net.Socket;

/**
*
*/
Expand All @@ -11,4 +14,38 @@ public Client(int port, String host) {
this.port = port;
this.host = host;
}

public void createConnection() {
try (Socket socket = new Socket("localhost", 5000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
) {
String outstr;
while (!socket.isClosed() && ((outstr = stdin.readLine()) != null)) {
//чтение
if (outstr.equalsIgnoreCase("exit")) {
break;
}
else //произвольная строка
{
out.println(outstr);
}
//запись
String s = in.readLine();
if (!s.isEmpty()) {
System.out.println(s);
}
}
}
catch(java.io.IOException e){
e.printStackTrace();
}
}

public static void main(String[] args) {
Client client = new Client(5000, "localhost");
client.createConnection();
}

}
65 changes: 65 additions & 0 deletions L7_threads/src/main/java/ru/track/prefork/Server.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,77 @@
package ru.track.prefork;

import sun.misc.IOUtils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;

/**
*
*/
public class Server {
private int port;

ConcurrentHashMap<Long, String> mmap;

public Server(int port) {
this.port = port;
mmap = new ConcurrentHashMap<>();
}

public static String getName (long num, Socket sock) {
return String.format("Client[%d]@[%s]:[%d]",
num,
sock.getInetAddress().getHostName(),
sock.getPort());
}

public void proceccSocket(final Socket client, long id) {
try (
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
) {
mmap.put(id, getName(id, client));

System.out.println("Connected: " + mmap.get(id));

String inpStr;
while (null != (inpStr = in.readLine())) {
if (inpStr.equalsIgnoreCase("exit")) {
client.close();
break;
}
out.println(inpStr);
}

} catch (java.io.IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Server server = new Server(5000);
AtomicLong counter = new AtomicLong(1);
ExecutorService pool = Executors.newFixedThreadPool(20);

try (ServerSocket serverSocket = new ServerSocket(5000)) {
Socket socket;
while (true) {
socket = serverSocket.accept();
final Socket client = socket;
pool.submit(new Thread(() -> {
Thread.currentThread().setName(getName(counter.get(), client));
server.proceccSocket(client, counter.getAndIncrement());
}));
}
} catch(java.io.IOException e){
e.printStackTrace();
}
}
}