Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Static run method in Runnable?

807607Nov 9 2006 — edited Nov 10 2006
Hello all, i've been attempting to code a multithreaded server program. I wanted a new handler class created to handle each connection in a new thread. To avoid race conditions, I needed to use a synchronized function to access a central class which holds all of the server's user information, etc, so I made processMessage() in the server class containing main(). I had processMessage declared a public synchronized void, non-static, but when i try to call it from run() i get a "non-static cannot be accessed blah blah blah" error. I fixed the problem by making processMessage static so the program actually works, but all I was wondering was why, when the run method is non-static do I need to make processMessage static? Thanks in advance.

Ah, and here's the code:
import java.net.*;
import java.io.*;

public class server
{
	public static void main(String args[])
	{
		
		ServerSocket MyService = null;
		Socket serversocket = null;
		Thread thread = null;
		// an input stream
		// has methods useful for recving from a socket
		DataInputStream input=null;
		
		// an output stream
		// has methods useful for sending over a socket
		PrintStream output=null;
		
		try
		{
			MyService = new ServerSocket(6667);
			while(true)
    		{
        		serversocket = MyService.accept();
        		thread = new Thread(new handler(serversocket));
        		thread.start();
        		thread.suspend();
        		thread.resume();
    		}
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
		// need to close streams before closing socket
		try
		{
       		MyService.close();
    	} 
    	catch (IOException e)
    	{
       		System.out.println(e);
    	}




	} // end main
	
	public static synchronized String processMessage(String msg)
	{
		
		return "";
	}
}

class handler implements Runnable
{
	Socket fd=null;
	DataInputStream input = null;
	PrintStream output=null;
	public handler(Socket socket)
	{
		System.out.println("in constructor");
		fd = socket;
		try
		{
			input = new DataInputStream(fd.getInputStream());
        	output = new PrintStream(fd.getOutputStream());
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}
	
	public void run()
	{
		System.out.println("in run");
		try
		{
			String temp = "";
			while(!temp.equals("quit"))
			{
				temp = input.readLine();
				server.processMessage(temp);
				System.out.println(temp);
			}
			output.close();
       		input.close();
			fd.close();
			
			Thread.currentThread().resume();
			Thread.currentThread().stop();
		}
		catch(IOException e)
		{
			System.out.println(e);			
		}
	} // end run
} // end class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 8 2006
Added on Nov 9 2006
13 comments
1,013 views