I've read some tutorials online and finding it hard to follow and they all refer to setting up a new class and running the threads on the main. I don't have a main as I'm building a backend for a GUI and I am just working to an interface.
I have pasted a sample of code below, to try and demonstrate what I want do.
public class TestClass {
public void testMethod(){
Queue<List<Long>> q = new ArrayDeque<>();
List<Long> testList = new ArrayList<>();
testList.add(9L);
q.add(testList);
while(q.size()>0){
//take off list from front of queue
//manipulate list and return it to queue/Delete it if no more to add and not reached a threshold.
}
So basically I'd like to call the while loop as a thread as I want to be able to control the time it runs for and also get some efficiency.
Can someone advise please?
Thanks
Change your while
loop to something like this.
Thread t = new Thread(){
public void run() {
while (q.size() > 0) {
// take off list from front of queue
// manipulate list and return it to queue/Delete it if no more to
// add and not reached a threshold.
}
//all other code
};
};
t.start();
Or you can wait for the thread to finish execution but that again blocks your testMethod until your while loop finishes. I don't think this is what you need.
Thread t = new Thread(){
public void run() {
while (q.size() > 0) {
// take off list from front of queue
// manipulate list and return it to queue/Delete it if no more to
// add and not reached a threshold.
}
};
};
t.start();
t.join();//waits for the thread to finish execution.
//rest of your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.