We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i am trying to understand the implementation of threads. In the code below
hi from thread
never appears in the console. Can someone correct my code.
int Val1;
MyThread thread;
void setup()
{
thread =new MyThread();
size(400,400);
thread.start();
}
void draw()
{
Val1= mouseX;
println(Val1);
}
public class MyThread extends Thread
{
public void start()
{
super.start();
}
public void run()
{
println("hi from thread");
}
}
Answers
It shows up here! Just stop the sketch soon and scroll all the way up! :-\"
Yup. I see it too.
Also, you don't need to override the start() method if you're just going to call super.start() from it anyway.
Thanks @GoToLoop & @KevinWorkman. So i understood the threads good enough. Now the next thing i want to do is i want to pass the value of mouseX to my thread and print the value using the thread. Please help me make suitable amendments to the code for achieving this.
Your MyThread is a sketch's inner class. Thus all of Processing's API, including mouseX, is accessible in it!
Output of above code looks like below
while (true) {}
&for ( ;; ) {}
.great explanation @GoToLoop . I am aware of the quick way to execute thread using thread("") method. Is there any difference in the efficiency or performance while using the inbuilt method vs extending our own thread class. Actually i am just building my understanding to finally make a code that runs a openCV face tracking in one thread and recording values in a .csv file in another one. Also there is another way of execution using runnable it would be great if you can have some quick commnts on that too.
AFAIK none! Method thread("") also instantiates a Thread for the invoked method name! :-\"
In short, we don't need a whole class in order to have a simple method running in its own Thread.
Interface Runnable is the base for class Thread and others and demands run() being implemented:
https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html
Since Java only allows 1
extends
per class, Runnable is useful for saving our 1-allowed inheritance!@GoToLoop This is in context with the threads. In situations when the function to be run has thread has no arguments everything is fine. But today i tried a thread with arguments by writing thread("arbitararyfunc(boolean state)")
i wrote the function as above in the setup. Then where i wrote the function, within the function i made an indefinite loop so that the thread stays alive. but the program says There is no public arbitararyfunc(boolean state) method in the class. How should i tackle this situation
Compare your MyThread's run() method:
w/ function myThread() from my thread("") usage example:
If you spotted right, it's the lack of an infinite loop wrap up! :-\"
Remember, once run() reaches the end, the Thread instance is over and can't even be restarted! :-S
hi @GoToLoop if i run my code i get error for the
thread =new MyThread();
i understand that i have to put the void run in the infinite loop. But problem may be with how to call this thread. if the problem would have been the non usage of infinite loop the code should have run for once. Pls copy the code in ur processing ide. you will find red mark under line 11.Indeed I didn't run your code b/c I've spotted the lack of an infinite loop inside its run() method.
Problem is you specified a constructor for your class MyThread, demanding a
boolean
to be passed to it!Just delete the constructor. And while you're at it, delete your empty start() too! ;-)
I did what you said. but now it is throwing null pointer exception.
new
in order to instantiate MyThread!oh! its getting confusing. Is this what u mean. it is saying sketch1345.MyThread is undefined. :(
I would be greatful if u could make a mockup. where i cud fill in my pieces of code. #-o i hope i am not bothering u much. thanks for trying hard to fix things ^:)^
Since I don't have the Serial hardware, most I can do is mockups! 8-|
I dunno whether it's gonna work, but here's my tweaked version: :-\"
Since serialEvent() already happens in a separate Thread (or not), you don't need your own Thread class
nor even use thread("") I guess: (:|
P.S.: Just found out that even though Serial runs under a separate Thread,
its callback serialEvent() is delegated to the sketch's "Animation" Thread.
It registers itself (via registerMethod() method) over sketch's pre() & dispose() call backs.
A potential simplification of serialEvent() callback.
Even less sure whether it's got any chance to work though: :(|)
P.S.: If Serial depends on pre(), which is called just before draw(), noLoop() + redraw() combo isn't gonna work!
So I had to remove that approach! Ignore the other examples! =(
Thanks. Let me try this and i will revert back. i have sent you a link. if you could pls visit that http://forum.processing.org/two/discussion/9840/i-want-to-display-the-temperature-and-video-but-video-is-hanging#latest
@GoToLoop. i tested the last code you posted. it is working well. However my problem for which i am trying the concept of threads remains the same. as i mentioned before i am taking the pain of playing with threads only because i want to display video from webcam in addition to the sensor value. This code suffices the condition but as soon as i will add the code for video display it will go out of context. noloop() and redraw() will not allow me to run the video. I think probably i could not convey to you y i am going for threads, otherwise the code is ok
going back to the older approach look at the code below, i omitted the serial as you donot have a hardware. I am going one step at a time so that i don't confuse you and can understand better what u are trying to say. If you will run this code (now you will be able to since no serial here). The thread prints hello i am your loving thread once. If i change the value of global variable at the top. The thread does't execute. That means the passing of value to thread is happening. great!!!!!!!!
Next hurdle is to automate this passing from Serial function to the thread. The SeriaEvent() function by default returns the value of the gottemp to the draw(). and we don't need to define any return type.look at the code below. But since now we have to return the value to the thread. I think we need to make two amendments one is changing the function definition from void to boolean and adding a return at end. Look at the code below i did this.
but unfortunatly on the console i am getting the debugging text i wrote in the serial function but not getting the debugging text from thread. What am i missing?