0

What constitutes Context Switching? I know it can be by using sleep() at times. I came across an example using a combination of wait() and notify() to carry out inter-thread communication so that one thread (of Producer class) supplies a data set and then waits till the another thread (of Consumer class) has consumed it.

My question is regarding performance implication where it is argued that "CPU wastes more time doing context switching and less time doing actual work". But looking at this example I think that context switching is made to happen only when the CPU completes the desired set of operations. Then what is the logic behind above quoted statement? Please find the attached example (Courtesy: Osborne The Complete Reference Java).

/*An implementation of a producer and consumer.*/

class Q {
int n;
boolean valueSet = false;

synchronized int get() {
    if(!valueSet)
    try {
      wait();
    } catch(InterruptedException e) {
      System.out.println("InterruptedException caught");
    }
    System.out.println("Got: " + n);
    valueSet = false;
    notify();
    return n;
}

synchronized void put(int n) {
    if(valueSet)
    try {
        wait();
    } catch(InterruptedException e) {
        System.out.println("InterruptedException caught");
    }
    this.n = n;
    valueSet = true;
    System.out.println("Put: " + n);
    notify();
}
}


class Producer implements Runnable {
    Q q;
    Producer(Q q) {
        this.q = q;
        new Thread(this, "Producer").start();
    }
    public void run() {
        int i = 0;
        while(true) {
            q.put(i++);
        }
    }
}

class Consumer implements Runnable {
    Q q;
    Consumer(Q q) {
        this.q = q;
        new Thread(this, "Consumer").start();
    }
    public void run() {
        while(true) {
            q.get();
        }
    }
}


class PCFixed {
    public static void main(String args[]) {
        Q q = new Q();
        new Producer(q);
        new Consumer(q); 
        System.out.println("Press Control-C to stop."); 
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user2029261
  • 1
  • 3
  • 6
  • 1
    http://stackoverflow.com/questions/18964050/context-switching-in-java – Genti Saliu Feb 09 '16 at 13:23
  • The context switch is the loading and unloading memory of each thread, not your own context in your code – Ruan Mendes Feb 09 '16 at 13:25
  • you got this code from a book? it looks like it was written by somebody ignorant of how to use wait/notify. – Nathan Hughes Feb 09 '16 at 14:34
  • Yes I got it in a ebook. Is there a better way to use these methods Nathan?? – user2029261 Feb 09 '16 at 14:47
  • wait()/notify() are low-level primitives that are meant to be used in a very specific way to implement higher-level synchronization objects. You can read about it in the "guarded blocks" section of the Java tutorials: https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html – Solomon Slow Feb 09 '16 at 16:14
  • The biggest pitfall of wait()/notify() is the "lost notification" bug: A call to `o.notify()` _does not do anything at all_ unless some other thread is already waiting in an `o.wait()` call. Poorly designed programs often end up with one thread waiting forever for a notification that happened _before_ the thread called `o.wait()`. – Solomon Slow Feb 09 '16 at 16:17

1 Answers1

0

CPU wastes more time doing Context Switching and less time doing actual work.

This is because, during the context switching OS kernel has to save the the state of the cpu registers for the current thread, then load these registers with the correct state for the next thread that is next to be executed. This is an extra work that kernel has to do during context switching not related to application code.

Another side effect of this is, we loose the locality of references because of context switch.