I may be really stupid here, but essentially I have a buffer and a thread.
I’ve made the buffer to get a bunch of messages from the user, and I want to create a thread to constantly loop to get the messages from the buffer.
This sound super simple but for some reason it just isn’t working.
Both the buffer and the thread are normal Java classes (not main); Buffer and DisplayThread.
I just can’t seem to get my head around how they’re supposed to work together.
Is this right? Or am I missing something?
public class DisplayThread extends Thread{
DisplayThread Thread = new DisplayThread(Buffer);
Thread.start();
public void run(){
while(true){
//will do something
}
}
}
I’ve tried creating a buffer object in the main class, and then passing that in, but it still doesn’t work.
What am I missing?! It must be so obvious but I just can’t see it at all.
You’ve got the basic idea, but I’m not sure why you put the new DisplayThread(Buffer) and Thread.start() stuff in the body of the class. Basically, those lines should be called from a method. Lets say, for example, they’re supposed to be called from Main, you’d do something like this:
In this scenario, you’re creating the buffer object, and passing it to the DisplayThread. In your display thread, you would create an instance variable of a Buffer, and a constructor to set it, like so: