473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

threads scanner

oll3i
679 512MB
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
Apr 24 '07 #1
2 4383
r035198x
13,262 8TB
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
Apr 24 '07 #2
JosAH
11,448 Expert 8TB
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
Apr 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
0
by: Daniel Bass | last post by:
Symbol MC9000k scanner running Windows Mobile 2003. C# .Net (.Net CF) with OpenNetCf 1.2 installed. Latest Symbol SDK driving the scan engine stuff. I've extracted the barcode scanner handling...
9
by: Dan =o\) | last post by:
Hey guys, I wonder if you could please provide me with some ideas as to how to get around this problem. Symbol MC9000-k, Pocket PC 2003... With a scanner application I've written, data is...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
4
by: Gerry19 | last post by:
Hi All, I'm trying to monitor data passed from a USB Barcode scanner but I can't find any decent code examples of what I need to do, including any references I need to include. I know I need to...
3
by: thename1000 | last post by:
Hi, I'm trying to create this output: Input team 1's name: Team 1 Input team 1's ranking: 90.4 etc.
0
by: Qui Sum | last post by:
I write ip/port scanner so users using search on our LAN know if the particular ftp is online or not. The class I write has the following structure: public class IPScanner { public...
6
by: rotaryfreak | last post by:
Hi everyone, ive had this problem for a while and i cant seem to figure out why. I am using eclipse to create my java code. When import the Scanner class, create a new object and so on... ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.