Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why implementing Runnable gives (error) where extending Thread class does not , (In )  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,

I can not understand why , when i extend Thread class on ClientHandler it does not gives me an error but when i implement Runnable interface it gives me a compile time error

Here is the code ----

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

class Server implements Runnable
{
private ClientHandler ch;
private Thread t; public boolean isRequest=false;
private Socket sk ; PrintWriter wr;

public void SktWriter(String temp)
{
if(isRequest)
{
wr.println("Server says::" + temp);
wr.flush();
}
}

public void Start() throws IOException
{
System.out.println("Port Listener is going to Start !!! ");
t = new Thread(this, "Port_Listener");
t.start();
}

public void run()
{
try
{
ServerSocket sS = new ServerSocket(5999);
for(;
{
sk=sS.accept();
wr = new PrintWriter(sk.getOutputStream(), true);
if(sk!=null)
new ClientHandler().start();
}
}catch(Exception e){
System.out.println("Problem in port allocation");
}
}
____________________________________________________________________________


Down here when i write [ ClientHandler extends Thread ], the code compiles but when i write [ ClientHandler implements Runnable ] the code gives a compile time error
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Demo.java:36: cannot resolve symbol
symbol : method start ()
location: class Server.ClientHandler
new ClientHandler().start();
^
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
____________________________________________________________________________
|=>class ClientHandler extends Thread
{
private String str;

public ClientHandler()
{
Thread th = new Thread(this, "ClientListener");
}

public void run()
{
try
{isRequest=true;
BufferedReader rd = new BufferedReader(new InputStreamReader(sk.getInputStream()));
for(;
{
while ((str = rd.readLine()) != null)
System.out.println(str);
}
} catch (IOException e) {
System.err.println(e);
}
}// end run
}// end ClientHandler
}// end Server



class Demo
{
public static void main(String arf[]) throws Exception
{
String t = "start" ;
Server ob1 = new Server();
ob1.Start() ;

while(true)
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
t = br.readLine();
ob1.SktWriter(t);
}
}
}

Hope i explained the point .


Regards
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Demo.java:36: cannot resolve symbol
symbol : method start ()
location: class Server.ClientHandler
new ClientHandler().start();


The error actually explains everything.

In the run() method of the Server class, you have the line, that basically uses the ClientHandler class as a Thread class -- not a Runnable class.

If you want to work with runnables, you need to change the line to something like:

new Thread(new ClientHandler()).start();

Henry
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually he covered that by providing a start method that makes a thread and starts it. But (if this is cut & paste code and not just a typo) the method has upper case S while it's called with a lower case start().
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry James ,

I didn't get you , The Start() method of Server class is a normal member function , In that function i am instatiating an object of thread and calling the run method . This run method further instantiate an object of thread class if it gets a request from a client .


I will be thankful if you kindly explain where am i wrong in the code

regards
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have two choices, you can make the change that I suggested, or you can make the change that Stan suggested. From your code, it looks like either change should work.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!