Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: is this mutlithreaded?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question is this mutlithreaded?

    Well I wrote single threaded server and then I followed a tutorial on how to make something multithreaded but to me it seems like I am just calling stuff through a lot of different methods and has not done anything to help.

    heres my code:
    package server;
     
    import java.net.*;
    import java.io.*;
     
    import server.writeFiles.Writer;
     
    public class Server implements Runnable {
     
    	static int port = 5555;
    	static ServerSocket server;
    	static Socket client;
    	static DataInputStream in;
    	public static String message1;
    	public static String message2;
     
    	public static void main(String[] args) throws IOException {
    		Server s = new Server();
    		s.run();
    	}
     
    	@Override
    	public void run() {
    		try {
    			while (true) {
    				try {
    					startServer();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				Thread.sleep(1000L);
    			}
    		} catch (InterruptedException iex) {
    		}
    	}
     
    	public void startServer() throws IOException {
    		server = new ServerSocket(port);
    		System.out.println("Starting server on port " + port);
    		while (true) {
    			client = server.accept();
    			System.out.println("Connection");
    			in = new DataInputStream(client.getInputStream());
    			message1 = in.readUTF();
    			System.out.println("Recived:" + message1);
    			message2 = in.readUTF();
    			System.out.println("Recived:" + message2);
    			Writer.writeFile();
    		}
    	}
     
    }

    So my question is. Is this mutlithreaded? Can it handle multiple connections at once?

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,261
    Thanks
    176
    Thanked 820 Times in 763 Posts
    Blog Entries
    5

    Default Re: is this mutlithreaded?

    No this is not multithreaded. You must create a new thread with a Runnable implementation, and start that thread. For example, after server.accept(), pass the obtained socket to a new class the implements Runnable which deals with the IO of that Socket, use that Runnable to create a new thread. This allows the server to return to the accept method and wait for another connection.
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)