简体   繁体   English

线程问题:程序无法启动

[英]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. 好的,这就是我所拥有的:我正在尝试在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: 这是setup()方法:

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; 1)框架甚至没有显示出来; 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 . 2)由于mySupportClass上的数据发生更改,因此我不确定所使用的赋值方法。

Searching in StackOverflow I didn't find anything about parsing values at a thread [In processing], so... Here I am! 在StackOverflow中搜索时,我没有发现任何有关在线程中解析值的信息[正在处理中],所以...在这里!

Any tip? 有小费吗?

@Override the superclass run() method to add your thread code. @重写超类run()方法以添加您的线程代码。 If you want to have your very own start() method with parameters, fine, but do not call run(), call start(). 如果要使用参数自己的start()方法,可以,但是不要调用run(),请调用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. 首先,线程永远不会执行,因为除非您自己调用,否则重载方法run(supportClass var)将被Thread类忽略。 In fact, the Thread class is expecting a run method with no arguments to execute. 实际上,Thread类期望没有参数的run方法执行。

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. 您没有覆盖Thread类的start方法。 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). Thread类的start方法将当前线程状态从New状态(刚刚创建)更改为Runnable状态(准备通过调用run方法执行)。 When the thread is in Runnable state, it can be chosen at any time by the JVM scheduler to be executed. 当线程处于可运行状态时,可以由JVM调度程序随时选择执行该线程。

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. 因此,您有2件事要做(不更改代码):首先在start(supportClass var)方法中,只需调用super.start()使其处于Runnable状态。 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. 并删除run(var)因为正如我所说,调用线程不是程序员的工作,而是JVM调度程序。 And define a run method with no arguments. 并定义一个不带参数的run方法。

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. 如果每个SupportClass实例需要一个线程(最简单),则可以在构造函数中传递它们。

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 http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html
  2. http://docs.oracle.com/javase/tutorial/essential/concurrency/ http://docs.oracle.com/javase/tutorial/essential/concurrency/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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