Connecting Tech Pros Worldwide Forums | Help | Site Map

threads scanner

oll3i's Avatar
Site Addict
 
Join Date: Mar 2007
Posts: 563
#1: Apr 24 '07
Expand|Select|Wrap|Line Numbers
  1. package zad31;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.locks.Lock;
  9. import java.util.concurrent.locks.ReentrantLock;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13. import com.sun.java_cup.internal.runtime.Scanner;
  14.  
  15. class Interruptible  {
  16.  
  17.       Lock lock = new ReentrantLock();
  18.  
  19.       Runnable task1 = new Runnable() {
  20.          public void run() {
  21.            System.out.println("Task 1 begins");
  22.            try {
  23.              lock.tryLock(1000, TimeUnit.SECONDS);  // próba zamknięcia rygla (czeka na wolny rygiel lub 1000 sekund)
  24.              System.out.println("Task 1 entered");
  25.            } catch(InterruptedException exc) {
  26.                System.out.println("Task 1 interrupted");
  27.            }
  28.            System.out.println("Task 1 stopped");
  29.          }
  30.       };
  31.  
  32.       Runnable task2 = new Runnable() {
  33.         public void run() {
  34.           System.out.println("Task 2 begins");
  35.           for (int i=1; i <= 600; i++) {
  36.             if (Thread.currentThread().isInterrupted()) break;
  37.             // jakieś obliczenie
  38.             if (Thread.currentThread().isInterrupted()) break;  // chcemy przerwać możliwie najszybciej
  39.             try {                                               // sleep() jest przerywane pzrez interrupt()!
  40.               Thread.sleep(1000);
  41.             } catch (InterruptedException exc) { break; }
  42.           }
  43.           System.out.println("Task 2 stopped");
  44.         }
  45.       };
  46.  
  47.  
  48.       Runnable task3 = new Runnable() {
  49.         Scanner scan = new Scanner( 
  50.                         new FileInputStream(FileDescriptor.in).getChannel(), "Cp852");
  51.         public void run() {
  52.           System.out.println("Task 3 begins");
  53.           System.out.print(">>");
  54.           while ((scan).hasNextLine()) {
  55.             try {
  56.               String s = scan.nextLine();
  57.               System.out.print('\n'+s + "\n>>");
  58.             } catch (Exception exc) {
  59.                 // Uwaga: scanner nie zgłasza wyjątków, ale przerywa dzialanie
  60.                 exc.printStackTrace();
  61.                 break;
  62.             }
  63.           }
  64.           System.out.println("Task 3 stopped - " + scan.ioException());  // jaki wyjątek go przerwał?
  65.         }
  66.       };
  67.  
  68.       Interruptible() {
  69.         ExecutorService exec = Executors.newCachedThreadPool();
  70.  
  71.         exec.execute(new Runnable() {        // wątek zamyka rygiel
  72.                         public void run() {
  73.                           lock.lock();
  74.                         }
  75.                      }
  76.          );
  77.         exec.execute(task1);
  78.         exec.execute(task2);
  79.         exec.execute(task3);
  80.         JOptionPane.showMessageDialog(null, "Press Ok to stop all tasks");
  81.         exec.shutdownNow();
  82.       }
  83.  
  84.     }
  85.  
  86.  
  87.  
why eclipse says it cannot instantiate the type Scanner
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Apr 24 '07

re: threads scanner


Quote:

Originally Posted by oll3i

Expand|Select|Wrap|Line Numbers
  1. package zad31;
  2.  
  3. import java.io.FileDescriptor;
  4. import java.io.FileInputStream;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.TimeUnit;
  8. import java.util.concurrent.locks.Lock;
  9. import java.util.concurrent.locks.ReentrantLock;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13. import com.sun.java_cup.internal.runtime.Scanner;
  14.  
  15. class Interruptible {
  16.  
  17.      Lock lock = new ReentrantLock();
  18.  
  19.      Runnable task1 = new Runnable() {
  20.      public void run() {
  21.      System.out.println("Task 1 begins");
  22.      try {
  23.      lock.tryLock(1000, TimeUnit.SECONDS); // próba zamknięcia rygla (czeka na wolny rygiel lub 1000 sekund)
  24.      System.out.println("Task 1 entered");
  25.      } catch(InterruptedException exc) {
  26.      System.out.println("Task 1 interrupted");
  27.      }
  28.      System.out.println("Task 1 stopped");
  29.      }
  30.      };
  31.  
  32.      Runnable task2 = new Runnable() {
  33.      public void run() {
  34.      System.out.println("Task 2 begins");
  35.      for (int i=1; i <= 600; i++) {
  36.      if (Thread.currentThread().isInterrupted()) break;
  37.      // jakieś obliczenie
  38.      if (Thread.currentThread().isInterrupted()) break; // chcemy przerwać możliwie najszybciej
  39.      try { // sleep() jest przerywane pzrez interrupt()!
  40.      Thread.sleep(1000);
  41.      } catch (InterruptedException exc) { break; }
  42.      }
  43.      System.out.println("Task 2 stopped");
  44.      }
  45.      };
  46.  
  47.  
  48.      Runnable task3 = new Runnable() {
  49.      Scanner scan = new Scanner( 
  50.      new FileInputStream(FileDescriptor.in).getChannel(), "Cp852");
  51.      public void run() {
  52.      System.out.println("Task 3 begins");
  53.      System.out.print(">>");
  54.      while ((scan).hasNextLine()) {
  55.      try {
  56.      String s = scan.nextLine();
  57.      System.out.print('\n'+s + "\n>>");
  58.      } catch (Exception exc) {
  59.      // Uwaga: scanner nie zgłasza wyjątków, ale przerywa dzialanie
  60.      exc.printStackTrace();
  61.      break;
  62.      }
  63.      }
  64.      System.out.println("Task 3 stopped - " + scan.ioException()); // jaki wyjątek go przerwał?
  65.      }
  66.      };
  67.  
  68.      Interruptible() {
  69.      ExecutorService exec = Executors.newCachedThreadPool();
  70.  
  71.      exec.execute(new Runnable() { // wątek zamyka rygiel
  72.      public void run() {
  73.      lock.lock();
  74.      }
  75.      }
  76.      );
  77.      exec.execute(task1);
  78.      exec.execute(task2);
  79.      exec.execute(task3);
  80.      JOptionPane.showMessageDialog(null, "Press Ok to stop all tasks");
  81.      exec.shutdownNow();
  82.      }
  83.  
  84.     }
  85.  
  86.  
  87.  
why eclipse says it cannot instantiate the type Scanner

Why not just use the scanner class in the util package
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Apr 24 '07

re: threads scanner


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
Reply