Is there anyway one kernel thread can switch between compute intensive tasks?
kant kodali
kanth909 at gmail.com
Thu Jan 25 09:51:07 UTC 2018
I am trying to see if there anyway a single thread in Java can switch
between tasks where each task is an infinite loop ?
I have the following code and I am wondering if there is any possible way I
could make the count for all three jobs below change while they run on
single thread? perhaps using wait/notify/interrupt (whatever it takes)?
import java.util.concurrent.ExecutorService;import
java.util.concurrent.Executors;
class Job implements Runnable {
protected int count;
public Job(){
this.count = 0;
}
public void run() {
System.out.println(Thread.currentThread().getName());
while(true) {
this.count = this.count + 1;
System.out.print("");
}
}}
public class ThreadTest {
static int tasks = 3;
static Job[] jobs = new Job[3];
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(1);
for (int i = 0; i < tasks; i++) {
jobs[i] = new Job();
executor.execute(jobs[i]);
}
while (!executor.isTerminated()) {
for (int i = 0; i < tasks; i++) {
System.out.print(jobs[i].c + " ");
}
System.out.println();
try { Thread.sleep(1000); } catch (InterruptedException ex) { }
}
System.out.println("end");
}}
If you actually run the program the count will only change for one job but
not for all three jobs. But if I use green threads in Golang and assign
each green thread an infinite loop they are able to switch however what I
don't understand is that once a green thread assigns a kernel thread to run
one infinite loop how come other green threads were able to schedule on the
same kernel thread? can someone enlighten me here? Sorry if my question is
naive.
Thanks much!
More information about the loom-dev
mailing list