hi all
Here i am trying to a code in which the run method is not being called when the thread is started..
Can anyone help me please..
Here is my code

import java.awt.*;

public class Games extends Frame implements Runnable
{

  int x=250,y=470;
Thread t=new Thread();
   Games()
  {

   setVisible(true);
   setSize(500,500);
}


 public  void repeat()
  {
   System.out.println("repeat");       
   if(t==null) 
      {
          t=new Thread();
      }
  System.out.println("repeat start");
     t.start();
     
 }
    


  public void run()
  {
    System.out.println("run started");
    while(true)
 {
    try
     {
      Thread.sleep(30);
      System.out.println("hi");
     }
    catch(InterruptedException e)
    {
      repaint(); 
    }
  }  
  }


public void paint(Graphics g)
{
   y+=6;
  g.drawOval(x,y,30,30);
System.out.println("paint");
  repeat();  
}

public static void main(String args[])
{
 new Games();
 
}
}

Thanks in advance

Recommended Answers

All 16 Replies

Look at the constructor for the Thread class.
How does it connect to the code in your run method? How would the Thread object find your code? If you don't give the Thread class an object with a run() method, it will use its own.
For more info: Read the API doc for the run() method in the Thread class.

Look at the constructor for the Thread class.
How does it connect to the code in your run method? How would the Thread object find your code? If you don't give the Thread class an object with a run() method, it will use its own.
For more info: Read the API doc for the run() method in the Thread class.

As i cant extend two classes i used Runnable interface for that..

And i called start method for the run method to be called indirectly..
Isn't it right?

I believe the problem lies in the fact that you create a new instance of the Games class, but you never actually call .start() on it. It appears all the constructor does is set the visibility and size. You need to call .start() on the Games object you create (perhaps a this.start() call would suffice, but I'm not 100% sure).

Also, I'm not sure what the Thread t object you create is for? That will just create a generic Thread object, not a Games object.

i called start method for the run method to be called

Yes a run method is called, but it not your run method. The Thread class has its own run method that it will call.
Look at my previous post again.

Yes a run method is called, but it not your run method. The Thread class has its own run method that it will call.
Look at my previous post again.

so do u say me to check the signature of the run() method?

I believe the problem lies in the fact that you create a new instance of the Games class, but you never actually call .start() on it. It appears all the constructor does is set the visibility and size. You need to call .start() on the Games object you create (perhaps a this.start() call would suffice, but I'm not 100% sure).

Well immediately after the constructor the paint method will be invoked which is calling the repeat method and in turn calling run using t.start..

also it might be of use to put a try catch statement surrounding the 'repeat()' method.

also it might be of use to put a try catch statement surrounding the 'repeat()' method.

what's the problem with try catch embedded in repeat method ??

what's the problem with try catch embedded in repeat method ??

uhm this is the repeat method right?

public  void repeat()
  {
   System.out.println("repeat");       
   if(t==null) 
      {
          t=new Thread();
      }
  System.out.println("repeat start");
     t.start();
 
 }

i see no embedded try catch? or maybe you or me misunderstood each other

when i ran your app on my netbeans it kept throwing a error here at line 10 in method repeat() 't.start();' :IllegalThreadStateException. so it would be better if you catch the exception and act appropriately, then not catching it at all.

uhm this is the repeat method right?

public  void repeat()
  {
   System.out.println("repeat");       
   if(t==null) 
      {
          t=new Thread();
      }
  System.out.println("repeat start");
     t.start();
 
 }

i see no embedded try catch? or maybe you or me misunderstood each other

when i ran your app on my netbeans it kept throwing a error here at line 10 in repeat():IllegalThreadStateException. so it would be better if you catch the exception and act appropriately, then not catching it at all.

oh yeah i think i misunderstood u
sorry for that.
thanks for the reply.
i did my code run by removing the thread concept..

oh yeah i think i misunderstood u
sorry for that.
thanks for the reply.
i did my code run by removing the thread concept..

Awesome man nothing better then when you get your code working by yourself :). if this thread is solved please mark it as such to avoid new posts on old/solved threads.

Awesome man nothing better then when you get your code working by yourself :). if this thread is solved please mark it as such to avoid new posts on old/solved threads.

ok sure

but what is the exception handler (catch (....))i should use to remove the exception u got in ur net beans?

ok sure

but what is the exception handler (catch (....))i should use to remove the exception u got in ur net beans?

try {
//starting of thread etc here
} catch (IllegalThreadStateException ex) {
//exception handling here
}

The try should be inside your method and mainly it must surround the 't.start();' line.

however if you want to catch any type of exception surround the inner of repeat() method with:

try {
//starting of thread etc here
//any other code
} catch (Exception ex) {//any exception that occurs will be caught
//exception handling here
}

do u say me to check the signature of the run() method?

Not the run method, the Thread constructor.

try {
//starting of thread etc here
} catch (IllegalThreadStateException ex) {
//exception handling here
}

The try should be inside your method and mainly it must surround the 't.start();' line.

however if you want to catch any type of exception surround the inner of repeat() method with:

try {
//starting of thread etc here
//any other code
} catch (Exception ex) {//any exception that occurs will be caught
//exception handling here
}

thank you
anyhow the problem is solved and thanks for the additional information :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.