multithreading - Java - Run a method on Main thread, called from worker thread -
how can call , run method on main thread, called worker thread?
main thread code, (foo() function accessible main thread):
thread newthread = new thread(mythread, mythread.getthreadname()); newthread.start();
worker thread code (newthread):
@override public void run(){ // need call here foo() function - has run on main thread }
thanks!
the words "call method in thread" have no meaning in java.
you need understand thread
, thread 2 different things: thread path of execution through code. thread
java object can used start new thread , manage life cycle.
a new thread begins when other thread calls t.start()
t
refers thread
object. thread begins executing t.run()
method, , wanders , out of function calls until reaches end of t.run()
, @ point dies. meanwhile, other threads following own paths through code.
at lowest level, only way 1 thread interact updating fields of shared objects , classes.
thread can tell thread b execute function or sending message (i.e., updating field in object), thread can never make thread b something. thread b can code executing says do. if code says, @ field f
, , if value greater zero, call function foobar()
, thread b do. or if code tells pop runnable
off of queue , call runnable's run()
method, thread do.
but no thread can change code other thread running once thread starts running it. can change fields influence code next.
Comments
Post a Comment