Cant get rid of this code:C:\\Baker Online\\Advanced Java\ etbeans\\ex_starts\\c
ID: 3558620 • Letter: C
Question
Cant get rid of this code:C:Baker OnlineAdvanced Java etbeansex_startsch22_ex3_NumberFindersrcNumberFinderRunnableApp.java:70: error: class, interface, or enum expected
}
1 error
Heres my code:
import java.util.ArrayList;
public class Monitor extends Thread
{
private ArrayList<Thread> threadList = new ArrayList<Thread>();
private static boolean isFound = false;
public void addThread(Thread t)
{
threadList.add(t); //added a thread object: notify the other threads.
}
public synchronized void foundNumber()
{
while(!isInterrupted()){
try
{
Thread.currentThread().interrupt();
Thread.currentThread().sleep(100);
}
catch(InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " is interrupted");
}
}
isFound = true;
}
public void run()
{
while(!isFound)
{
}
}
}
public class Main
{
public static void main(String args[])
{
int target = (int) (Math.random() * 1000);
System.out.println("The number is " + target);
Monitor threads = new Monitor();
threads.start();
Thread t1 = new Thread(new Finder(target, 0, 249));
Thread t2 = new Thread(new Finder(target, 250, 499));
Thread t3 = new Thread(new Finder(target, 500, 749));
Thread t4 = new Thread(new Finder(target, 750, 999));
t1.start();
t2.start();
t3.start();
t4.start();
threads.addThread(thread1);
threads.addThread(thread2);
threads.addThread(thread3);
threads.addThread(thread4);
}
}
public class Finder implements Runnable {
public Finder(int target, int i, int i0) {
}
private int targetNumber;
private int startSearch;
private int endSearch;
private int count=0;
private Monitor threadObject;
public Finder(Monitor thread, int targetNumber, int startSearch, int endSearch)
{
this.targetNumber = targetNumber;
this.startSearch = startSearch;
this.endSearch = endSearch;
this.threadObject = thread;
}
public void run()
{
Thread t = Thread.currentThread();
for(int a=startSearch; a < endSearch; a++)
{
if(targetNumber == a)
{
System.out.println("Target number " + targetNumber
+ " found by "+ t.getName());
threadObject.foundNumber();
}
count++;
if(count == 10)
{
try
{
t.sleep(1);
}
catch(InterruptedException e)
{
System.out.println(t.getName() + " interrupted");
}
count=0;
}
}
}
}
Explanation / Answer
Rename thread1, thread2, thread3 and thread4 to t1, t2, t3, t4 :) Also, you can have only one public class with Main() method in a Java file. So make Monitor and Finder static (This is needed so that you can instantiate Thread and Finder class variables from main, which is a static method). And then move these classes inside the Main class. So now these classes are static nested classes, which you can instantiate from inside Main() method without any problems. Save the file with name Main.java and you are good to go! I would vote against changing your jre to 1.5 since that is not the latest and I am sure you would run into problems when trying to execute this program on some other computer. Hope this helps! Cheers! :)
import java.util.ArrayList;
public class Main
{
public static class Monitor extends Thread
{
private ArrayList<Thread> threadList = new ArrayList<Thread>();
private boolean isFound = false;
public void addThread(Thread t)
{
threadList.add(t); //added a thread object: notify the other threads.
}
public synchronized void foundNumber()
{
while(!isInterrupted()){
try
{
Thread.currentThread().interrupt();
Thread.currentThread().sleep(100);
}
catch(InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " is interrupted");
}
}
isFound = true;
}
public void run(){
while(!isFound){}
}
}
public static class Finder implements Runnable {
public Finder(int target, int i, int i0) {
}
private int targetNumber;
private int startSearch;
private int endSearch;
private int count=0;
private Monitor threadObject;
public Finder(Monitor thread, int targetNumber, int startSearch, int endSearch)
{
this.targetNumber = targetNumber;
this.startSearch = startSearch;
this.endSearch = endSearch;
this.threadObject = thread;
}
public void run()
{
Thread t = Thread.currentThread();
for(int a=startSearch; a < endSearch; a++)
{
if(targetNumber == a)
{
System.out.println("Target number " + targetNumber
+ " found by "+ t.getName());
threadObject.foundNumber();
}
count++;
if(count == 10)
{
try
{
t.sleep(1);
}
catch(InterruptedException e)
{
System.out.println(t.getName() + " interrupted");
}
count=0;
}
}
}
}
public static void main(String args[])
{
int target = (int) (Math.random() * 1000);
System.out.println("The number is " + target);
Monitor threads = new Monitor();
threads.start();
Thread t1 = new Thread(new Finder(target, 0, 249));
Thread t2 = new Thread(new Finder(target, 250, 499));
Thread t3 = new Thread(new Finder(target, 500, 749));
Thread t4 = new Thread(new Finder(target, 750, 999));
t1.start();
t2.start();
t3.start();
t4.start();
threads.addThread(t1);
threads.addThread(t2);
threads.addThread(t3);
threads.addThread(t4);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.