• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Thread making

Aeren

n00b
Joined
Aug 29, 2005
Messages
37
Hi everyone!
I tried to make a Thread, but it doesn't work.
I have a class called NetMyThread, which implements Runnable.
Has a: private Thread myThread; as a variable, which is initialized in the init as follows:
Code:
myThread       = new Thread( this ); (init is called from the constructor)
myThread.start( );

the run function looks like this:
Code:
  public void run( ) {
    try {
      for (int i=0;i<5;i++){
        Thread.sleep( time );
        System.out.println("blahblah");
        differenceEngine( );
      }
    } catch( InterruptedException e ) { System.out.println( e.toString( ) ); }
  }
I can't see anything written out on the console. What's wrong with my code?

Thanks!

Aeren
 
when debugging, it is often useful to just catch everything with a catch(Exception) and then inside the body include e.printStackTrace();

Here's one of my working threads:
Code:
public class Blargh extends Thread {

	public Blargh() {
		System.out.println("Blargh instantiated, yarr!");
	}

	public void run() {
		System.out.println("Thread started!");
		while(true){
			try{
				System.out.println("Blorples");
				Thread.sleep(1000);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
	}
}

And here's how I call it:

Code:
blah = new Blargh();
new Thread(blah).start();
 
Well I copy pasted your code and the good news is that it runs fine. So the problem lies elsewhere. If you need more help just post up more code.

Cheers!
 
Nemezer said:
Well I copy pasted your code and the good news is that it runs fine. So the problem lies elsewhere. If you need more help just post up more code.

Cheers!

It runs for me as well, but it doesn!t write blahblah out at all. Did i do it right with the System.out.println?
 
Well yes, what I meant to say is that the code works fine for me. I copy pasted it and when i run it I can see the Thread working. Thus your trouble is something else that is not in the code you gave us.
 
Back
Top