In Java, I have a need to implement a class that extends Thread in another similar class. Is it possible?
An example of what I'm trying to do is the following (simplified) snippet:
// The first layer is a Thread public class Consumer extends Thread { // Variables initialized correctly in the Creator private BufferManager BProducer = null; static class Mutex extends Object {} static private Mutex sharedMutex = null; public Consumer() { // Initialization of the thread sharedMutex = new Mutex(); BProducer = new BProducer(sharedMutex); BProducer.start(); } public void run() { int data = BProducer.getData(); ///// .... other operations } ////// ...... some code // Also the second layer is a Thread private class BufferManager extends Thread { Mutex sharedMutex; int data; public BufferManager(Mutex sM) { sharedMutex = sM; } public int getData(Mutex sM) { int tempdata; synchronized(sharedMutex) { tempdata = data; } return tempdata; } public void run() { synchronized(sharedMutex) { data = getDataFromSource(); } ///// .... other operations on the data } } }
The second stream is implemented directly inside the first. Moreover, I would like to know if the implementation of Mutex will be implemented. If not, is there a better (standard) way to do this?
Thanks in advance.
2dvisio
source share