threads scanner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #1

    threads scanner

    Code:
    package zad31;
    
    import java.io.FileDescriptor;
    import java.io.FileInputStream;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    import javax.swing.JOptionPane;
    
    import com.sun.java_cup.internal.runtime.Scanner;
    
    class Interruptible  {
    
    	  Lock lock = new ReentrantLock();
    
    	  Runnable task1 = new Runnable() {
    	     public void run() {
    	       System.out.println("Task 1 begins");
    	       try {
    	         lock.tryLock(1000, TimeUnit.SECONDS);  // próba zamknięcia rygla (czeka na wolny rygiel lub 1000 sekund)
    	         System.out.println("Task 1 entered");
    	       } catch(InterruptedException exc) {
    	           System.out.println("Task 1 interrupted");
    	       }
    	       System.out.println("Task 1 stopped");
    	     }
    	  };
    
    	  Runnable task2 = new Runnable() {
    	    public void run() {
    	      System.out.println("Task 2 begins");
    	      for (int i=1; i <= 600; i++) {
    	        if (Thread.currentThread().isInterrupted()) break;
    	        // jakieś obliczenie
    	        if (Thread.currentThread().isInterrupted()) break;  // chcemy przerwać możliwie najszybciej
    	        try {                                               // sleep() jest przerywane pzrez interrupt()!
    	          Thread.sleep(1000);
    	        } catch (InterruptedException exc) { break; }
    	      }
    	      System.out.println("Task 2 stopped");
    	    }
    	  };
    
    
    	  Runnable task3 = new Runnable() {
    	    Scanner scan = new Scanner( 
    	                    new FileInputStream(FileDescriptor.in).getChannel(), "Cp852");
    	    public void run() {
    	      System.out.println("Task 3 begins");
    	      System.out.print(">>");
    	      while ((scan).hasNextLine()) {
    	        try {
    	          String s = scan.nextLine();
    	          System.out.print('\n'+s + "\n>>");
    	        } catch (Exception exc) {
    	            // Uwaga: scanner nie zgłasza wyjątków, ale przerywa dzialanie
    	            exc.printStackTrace();
    	            break;
    	        }
    	      }
    	      System.out.println("Task 3 stopped - " + scan.ioException());  // jaki wyjątek go przerwał?
    	    }
    	  };
    
    	  Interruptible() {
    	    ExecutorService exec = Executors.newCachedThreadPool();
    
    	    exec.execute(new Runnable() {        // wątek zamyka rygiel
    	                    public void run() {
    	                      lock.lock();
    	                    }
    	                 }
    	     );
    	    exec.execute(task1);
    	    exec.execute(task2);
    	    exec.execute(task3);
    	    JOptionPane.showMessageDialog(null, "Press Ok to stop all tasks");
    	    exec.shutdownNow();
    	  }
    
    	}
    why eclipse says it cannot instantiate the type Scanner
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by oll3i
    Code:
    package zad31;
     
    import java.io.FileDescriptor;
    import java.io.FileInputStream;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
     
    import javax.swing.JOptionPane;
     
    import com.sun.java_cup.internal.runtime.Scanner;
     
    class Interruptible {
     
    	 Lock lock = new ReentrantLock();
     
    	 Runnable task1 = new Runnable() {
    	 public void run() {
    	 System.out.println("Task 1 begins");
    	 try {
    	 lock.tryLock(1000, TimeUnit.SECONDS); // próba zamknięcia rygla (czeka na wolny rygiel lub 1000 sekund)
    	 System.out.println("Task 1 entered");
    	 } catch(InterruptedException exc) {
    	 System.out.println("Task 1 interrupted");
    	 }
    	 System.out.println("Task 1 stopped");
    	 }
    	 };
     
    	 Runnable task2 = new Runnable() {
    	 public void run() {
    	 System.out.println("Task 2 begins");
    	 for (int i=1; i <= 600; i++) {
    	 if (Thread.currentThread().isInterrupted()) break;
    	 // jakieś obliczenie
    	 if (Thread.currentThread().isInterrupted()) break; // chcemy przerwać możliwie najszybciej
    	 try { // sleep() jest przerywane pzrez interrupt()!
    	 Thread.sleep(1000);
    	 } catch (InterruptedException exc) { break; }
    	 }
    	 System.out.println("Task 2 stopped");
    	 }
    	 };
     
     
    	 Runnable task3 = new Runnable() {
    	 Scanner scan = new Scanner( 
    	 new FileInputStream(FileDescriptor.in).getChannel(), "Cp852");
    	 public void run() {
    	 System.out.println("Task 3 begins");
    	 System.out.print(">>");
    	 while ((scan).hasNextLine()) {
    	 try {
    	 String s = scan.nextLine();
    	 System.out.print('\n'+s + "\n>>");
    	 } catch (Exception exc) {
    	 // Uwaga: scanner nie zgłasza wyjątków, ale przerywa dzialanie
    	 exc.printStackTrace();
    	 break;
    	 }
    	 }
    	 System.out.println("Task 3 stopped - " + scan.ioException()); // jaki wyjątek go przerwał?
    	 }
    	 };
     
    	 Interruptible() {
    	 ExecutorService exec = Executors.newCachedThreadPool();
     
    	 exec.execute(new Runnable() { // wątek zamyka rygiel
    	 public void run() {
    	 lock.lock();
    	 }
    	 }
    	 );
    	 exec.execute(task1);
    	 exec.execute(task2);
    	 exec.execute(task3);
    	 JOptionPane.showMessageDialog(null, "Press Ok to stop all tasks");
    	 exec.shutdownNow();
    	 }
     
    	}
    why eclipse says it cannot instantiate the type Scanner
    Why not just use the scanner class in the util package

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      I'm almost sure the OP uses Java 1.5 or newer: the concurrent packages weren't
      there in Java 1.4, or the OP must've installed Doug Lea's concurrent packages
      manually which I don't believe for now.

      When Eclipse doesn't recognize a class because you haven't imported it yet
      or you haven't imported the entire package, Eclipse can search for it. When it
      finds more than one candidate it offers a choice. Eclipse found a Scanner in
      the com.sun.* package as well as the java.util package.

      It is never sensible to *not* read the choices offered by Eclipse and simply
      bang the enter key or to press the ok key. Reading is not (yet?) supposed to
      be a lost ancient form of art you know.

      kind regards,

      Jos

      Comment

      Working...