views:

182

answers:

3

I have 2 Threads, one that's polling a mailbox for messages then sleeping while (!quit) and another that's supposed to change the quit flag should the user enter 'Q'. It seems that the scanning Thread blocks the other Thread from executing until there's some input (usually 2 lines). I've tried changing the priority of the Threads and the order in which they start, to no avail.

class quitThread extends Thread {
  public void run() {
    char c;
    Scanner scanner = new Scanner(System.in);
    do {
      c = scanner.nextLine().toUpperCase().charAt(0);
    } while (c != 'Q');
    quit = true;
  }
}

class recieveThread extends Thread {
  public void run() {
    System.out.println("thread started");
    while (!quit) {
      try {
        MailHandler handler = new MailHandler();       
        handler.recieve();
        System.out.println("Sleeping");
        sleep(_sleepinterval);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

I know I have to worry about mutex and synchronisation, but I want the Threads to at least work before I start worrying about it. EDIT: This is how I'm starting the threads:

void go() throws Exception{
 char c;
 System.out.println("S or R");
 Scanner s = new Scanner(System.in);
 c = s.nextLine().toUpperCase().charAt(0);
 MailHandler handler = new MailHandler();    
 QuitThread q = new QuitThread();
 q.setPriority(java.lang.Thread.MIN_PRIORITY);
            RecieveThread rc = new RecieveThread();
        rc.setPriority(java.lang.Thread.MAX_PRIORITY);


 switch (c){
 case 'S':

  handler.send("[email protected]", "hello there");
  break;
 case 'R':
  rc.start();
  q.start();
  break;
 default :
  break;  


 }


}

Note: the priorities weren't originally there, it's something i just tried and it didn't make any difference.

A: 

As you can see they won’t work without synchronization, so fix that first.

Bombe
+3  A: 

How are two different instances of two different classes sharing the quit variable? Try using an AtomicBoolean, shared between your two threads.

Also, how do you know the scanning thread is blocking the other one? From your code, I can't see them sharing any resources except for the quit variable.

Maybe you see the "thread started" message and then you don't see the "sleeping" message for a while because the receiveThread is stuck in the handler.receive() method...

Chochos
I've tried leaving it for a while and it does nothing, I also have some messages coming from handler.recieve(). No matter how long i leave it, entering a couple of lines into the window causes handler.recieve to start.
your quitThread doesn't have a "thread started" message, so you don't really know for sure that it started. Maybe your program is getting stuck in the first nextLine, before you start the threads.
Chochos
A: 

Looks like its related to bug 4206767

Pyrolistical
According to the text of that bug, this is only an issue when using the classic VM and "Green Threads" and not native threads.
Eddie