简体   繁体   中英

Thread issue: program doesn't start

Ok guys, here's what I've got: I'm trying to make a thread-based program in Processing. What the program does is really simple, and I can deal with that, but when I tried to make it run in a thread, it takes fun of me!

Long story short, I've tried making something like this:

class supportClass{
   [All the junk.];
};
class threadClass extends Thread{
   boolean goingThread;
   [Some other junk.];
   threadClass(){
      goingThread = false;
      [Junk.]
   }
   void start(supportClass var){
      goingThread = true;
      run(var);
      goingThread = false;
   }
   void run(supportClass var){
      [Junk which does all the work!]
   }
};

And here's the setup() method:

void setup(){
   [Some junk init.];
   supportClass mySupportClass = new supportClass();
   threadClass myClass = new threadClass();
   myClass.start(mySupportClass);
}

So, here's the issues are two:

1) The frame doesn't even show itself; I mean: the program seems to not run at all...;

2) I'm not sure of the value-giving method that I'm using, because of the changing of data on mySupportClass .

Searching in StackOverflow I didn't find anything about parsing values at a thread [In processing], so... Here I am!

Any tip?

@Override the superclass run() method to add your thread code. If you want to have your very own start() method with parameters, fine, but do not call run(), call start().

First, your thread will never execute because your overloaded method run(supportClass var) will be ignored by the Thread class unless you call it yourself. In fact, the Thread class is expecting a run method with no arguments to execute.

Secondly, for the start method. In your code, you are providing your own start method. You are not overriding the start method of the Thread class. As consequences, your thread will never be to a runnable state. The start method of the Thread class changes the current thread state from a New state (just created) to be a to Runnable state (ready to be executed by calling the run method). When the thread is in Runnable state, it can be chosen at any time by the JVM scheduler to be executed.

So, you have 2 things to do (without changing your code) : first in the start(supportClass var) method, just call super.start() to make it in a Runnable state. And also remove the line run(var) because as I said it's not the programmer's job to call the Thread but the JVM scheduler. And define a run method with no arguments.

If you want to pass arguments to your thread class, it depends on your strategy. If you want one thread per instance of SupportClass (which the easiest), you can pass them in the constructor.

I would advise you to read those links to have more information about threads :

  1. http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
  2. http://docs.oracle.com/javase/tutorial/essential/concurrency/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM