0

It is me again, and I am pretty stuck with porting some code from c# to java. As I now read, there are two main-ways in order to use multithreading in java. There is the creation of a class, which inherits from thread, or the creation of a class, which implements Runnable. (There is also the anonymous approach, I know ).

As I now painfully had to recognize, that porting the multithreading from c# to java could get me to refactor some of my code ( which I would like to avoid as much as possible) .

So, let me say, I have a class CLQueueWorker inheriting from Thread. My parent object (_oParentMAIN ) will keep a reference of this object.

It's "run" method SHOULD do some asynch actions ( which until now are all implemented in _oParentMAIN):

1) dequeue some data from an object, which was filled (and is maintained) in _oParentMAIN -->(my idea is now to create a static variable in CLQueueWorker, and work with this instead of keeping it in the _oParentMAIN).

2) invokes a method-call on ui thread --> Do not know, how to refactor this, because until now This method is a member of _oParentMAIN and I will not be able to invoke its call on the ui thread, because CLQueueWorker does not know this member).

3) invokes another method on ui thread, which is responsible for ui-elements. --> here my problems also will be the same as in point 2.

So, I need some hints how to redesign this in the appropriate way. Let me say, is it possible, that CLQueueWorker (which really spawns another thread) calls methods/throws events to/of _oParentMAIN AND PASSES EXECUTION BACK INTO MAIN thread ?

I hope, I could describe it clearly enough.

icbytes
  • 1,831
  • 1
  • 17
  • 27
  • "I hope, I could describe it clearly enough." No :) And don't extends `Thread`, instead use `Runnable` because http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – zapl Dec 13 '13 at 16:50
  • Thx, I already changed that approach , before inheriting from thread i had the runnable approach ( i kept this class ). So i just delete the inherited one. How can I help You with more detailed informationn ? – icbytes Dec 13 '13 at 16:52
  • Your question as a whole is a bit broad and to really understand what you need would require to know your code. You also seem to assume that a method defined in a class named `Thread` is executed in a thread. That's not the case. Every method you call is executed within the calling thread. There is no way that threads simply switch. That's why you call the `start` method of a `Thread` instead of the `run` method (which is later on called from within the new thread). The problem is actually not whether A knows about a method in B. – zapl Dec 13 '13 at 16:59
  • This with start I know. I now read about loopers / and MessageLoops. The now to be created,second thread must do something and then announce the main thread, that it itself shall proceeed with the process ( and the main thread spawned the second thread). The second thread is a object , as child in the hierarchy, and therefore cannot access the methods, which must be invoked in the parent object(and on the main thread ). I will continue reading about Loopers. Though, I will try to provide some code in the next days ( 2 or 3 days ). – icbytes Dec 13 '13 at 17:04

0 Answers0