Skip to content

Commit

Permalink
Java Thread&Net demo
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiled committed Mar 23, 2014
1 parent ec8747d commit 7348f21
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 0 deletions.
35 changes: 35 additions & 0 deletions seminars/class07/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.CodeStyle.project.text-line-wrap>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaces-per-tab>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodDeclParen>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodDeclParen>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodCallParen>true</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.spaceBeforeMethodCallParen>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.tab-size>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>4</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.indent-shift-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>false</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.expand-tabs>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>80</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-limit-width>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.otherBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>none</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.text-line-wrap>
</properties>
</project-shared-configuration>
25 changes: 25 additions & 0 deletions seminars/class07/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ru.mipt</groupId>
<artifactId>class07</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>class07</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* ClientDemo.java
* Created on Mar 24, 2014
*/
package ru.mipt.spring2014.class07.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ClientDemo
{
private static final int PORT = 9999;

public static void main (String[] args)
{
for (int id = 1; id < 5; id++)
{
System.out.println ("Client: sending request #" + id);
System.out.println ("Client: received response: "
+ communicate (new Request (id, "Sample MesSage " + Math.random ())));
}
}

private static String communicate (Request request)
{
try{
final Socket connection = new Socket ("127.0.0.1", PORT);

try{
final ObjectOutputStream out = new ObjectOutputStream (new BufferedOutputStream (connection.getOutputStream ()));
out.writeObject (request);
out.flush ();

final ObjectInputStream in = new ObjectInputStream (new BufferedInputStream (connection.getInputStream ()));
return (String) in.readObject ();
} finally
{
connection.close ();
}
} catch (Exception e)
{
System.err.println ("Client: Error communicating with the server: " + e);
throw new RuntimeException ("Error communicating with the server", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Request.java
* Created on Mar 24, 2014
*/
package ru.mipt.spring2014.class07.net;

import java.io.Serializable;

public class Request implements Serializable
{
private final long id;
private final String body;

public Request (long id, String body)
{
this.id = id;
this.body = body;
}

@Override
public String toString ()
{
return "#" + id + ": <[" + body + "]>";
}

public long getId ()
{
return id;
}

public String getBody ()
{
return body;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* RequestHandler.java
* Created on Mar 24, 2014
*/
package ru.mipt.spring2014.class07.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class RequestHandler implements Runnable
{
private final Socket connection;

public RequestHandler (Socket connection)
{
this.connection = connection;
}

public void run ()
{
try{
final ObjectInputStream in = new ObjectInputStream (new BufferedInputStream (connection.getInputStream ()));
final ObjectOutputStream out = new ObjectOutputStream (new BufferedOutputStream (connection.getOutputStream ()));

Request req = (Request) in.readObject ();
System.out.println ("Server: Request received: " + req);
out.writeObject (req.getBody ().toUpperCase ());
out.flush ();
} catch (Exception e)
{
System.err.println ("Server: Error handling request: " + e);
} finally
{
try{
connection.close ();
} catch (IOException e)
{
System.err.println ("Server: Error closing connection: " + e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Server.java
* Created on Mar 24, 2014
*/
package ru.mipt.spring2014.class07.net;

import java.io.IOException;
import java.net.ServerSocket;

public class Server implements Runnable
{
private final int port;

public Server (int port)
{
this.port = port;
}

public void run ()
{
final ServerSocket socket;
try{
System.out.println ("Server: Start accepting connections");
socket = new ServerSocket (port);

try{
while (!Thread.interrupted ())
{
new Thread (new RequestHandler (socket.accept ())).start ();
System.out.println ("Server: Connection accepted");
}
} finally
{
System.out.println ("Server: Stop accepting requests");
socket.close ();
}
} catch (IOException ex)
{
System.err.println ("Server: Exception in server thread: " + ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* ClientDemo.java
* Created on Mar 24, 2014
*/
package ru.mipt.spring2014.class07.net;

public class ServerDemo
{
private static final int PORT = 9999;

public static void main (String[] args)
{
new Thread (new Server (PORT)).start ();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* ExecutorsDemo.java
* Created on Mar 19, 2014
*/
package ru.mipt.spring2014.class07.threads;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorsDemo
{
public static void main (String[] args) throws InterruptedException, ExecutionException
{
final ExecutorService execSrv = Executors.newFixedThreadPool (2);

final int tasksNumber = 6;
final Future<Double>[] tasks = new Future[tasksNumber];
System.out.println ("main: start creating tasks");
for (int i = 0; i < tasksNumber; i++)
{
tasks[i] = execSrv.submit (new SleepAction (i, 100L));
}
System.out.println ("main: all tasks created");

// execSrv.shutdown ();

// for (int i = 0; i < tasksNumber; i++)
// {
// System.out.println ("Task #" + i + " took " + tasks[i].get () + " ms");
// }

// execSrv.shutdown ();

System.out.println ("main: end");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* SleepAction.java
* Created on Mar 19, 2014
*/
package ru.mipt.spring2014.class07.threads;

import java.util.concurrent.Callable;

public class SleepAction implements Callable<Double>
{
private final int id;
private final long interval;

public SleepAction (int id, long interval)
{
this.id = id;
this.interval = interval;
}

@Override
public Double call () throws Exception
{
final long startTime = System.nanoTime ();

System.out.println ("Start task #" + id);
Thread.sleep (interval);
System.out.println ("Finished task #" + id);

return .001 * .001 * (System.nanoTime () - startTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* SleeperJob.java
* Created on Mar 19, 2014
*/
package ru.mipt.spring2014.class07.threads;

public class SleeperJob implements Runnable
{
private final long interval;
private final int maxWaits;

public SleeperJob (long interval, int maxWaits)
{
this.interval = interval;
this.maxWaits = maxWaits;
}

@Override
public void run ()
{
final long startTime = System.nanoTime ();
for (int i = 0; i < maxWaits && !Thread.interrupted (); i++)
{
System.out.println (getClass ().getSimpleName ()
+ ": wait for " + interval + " ms");
try
{
Thread.sleep (interval);
} catch (InterruptedException e)
{
final long waitTime = System.nanoTime () - startTime;
System.out.printf ("%s: interrupted after %.3f ms\n",
getClass ().getSimpleName (), .001 * .001 * waitTime);
return;
}
}
final long execTime = System.nanoTime () - startTime;
System.out.println (String.format ("%s: finished after %.3f ms",
getClass ().getSimpleName (), .001 * .001 * execTime));
}
}
Loading

0 comments on commit 7348f21

Please sign in to comment.