java.util.concurrent

L

Let me Think

Dear All
I'm fairly new to Java and have been attempting to get the
java.util.concurrent package to work.

I am using java version
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode, sharing)

Following many hours of reading the APIs and scouring the internet for
samples, have got stuck and am looking for help.

Below are files I have been creating as test files


ThreadPerTaskExecutor.java
ThreadOne.java
ThreadTwo.java

The idea is that when the method execute() in the class
ThreadPerTaskExecutor is called it will concurrently launch the code in

ThreadOne.java
ThreadTwo.java

So far
ThreadOne.java
ThreadTwo.java

Compile fine

However ThreadPerTaskExecutor.java fails to compile with the following
errors.


C:\test\ThreadPerTaskExecutor.java:5: test.ThreadPerTaskExecutor is not
abstract and does not override abstract method execute(java.lang.Runnable)
in java.util.concurrent.Executor
class ThreadPerTaskExecutor implements Executor {
^
C:\test\ThreadPerTaskExecutor.java:9: cannot find symbol
symbol : variable executer
location: class test.ThreadPerTaskExecutor
executer.execute(new test.ThreadOne());
^
C:\test\ThreadPerTaskExecutor.java:10: cannot find symbol
symbol : variable executor
location: class test.ThreadPerTaskExecutor
executor.execute(new test.ThreadTwo());

The Reason I am looking in to this is to be able to launch two or more
methods concurrently in robocode for performance reasons.

Any ideas what I have done wrong

Thanks







----------- file ThreadPerTaskExecutor.java --------


package test;

import java.util.concurrent.*;

class ThreadPerTaskExecutor implements Executor {

public void execute() {

executer.execute(new test.ThreadOne());
executor.execute(new test.ThreadTwo());

}
}






----------- file ThreadOne.java --------


package test;

import robocode.*;

public class ThreadOne {

public void run() {

System.out.println("External Thread one");

}

public void ThreadOne() {

System.out.println("External Thread Two");

}
}



----------- file ThreadTwo.java --------

package test;

import robocode.*;

public class ThreadTwo {

public void run() {

System.out.println("External Thread Two");

}

public void ThreadTwo() {

System.out.println("External Thread Two");

}

}
 
Z

zero

Dear All
I'm fairly new to Java and have been attempting to get the
java.util.concurrent package to work.

<snipped>

Rather than hand you the solution, I suggest you have a look at the text
of the errors.
C:\test\ThreadPerTaskExecutor.java:5: test.ThreadPerTaskExecutor is
not abstract and does not override abstract method
execute(java.lang.Runnable) in java.util.concurrent.Executor
class ThreadPerTaskExecutor implements Executor {
^

it says you need to override (ie write it yourself) method
execute(java.lang.Runnable)
You have a method
execute()
This is not the same!

C:\test\ThreadPerTaskExecutor.java:9: cannot find symbol
symbol : variable executer
location: class test.ThreadPerTaskExecutor
executer.execute(new test.ThreadOne());
^

This says that executer is unknown. Usually this means that you either
misspelled it, or forgot to declare it. A typical declaration is:
Object myObject = new Object();
after which you can use myObject.
A mistake a lot of newbies make is also that they forget Java is case-
sensitive. MyObject is not the same as myObject or myobject.
C:\test\ThreadPerTaskExecutor.java:10: cannot find symbol
symbol : variable executor
location: class test.ThreadPerTaskExecutor
executor.execute(new test.ThreadTwo());

see above.

One more thing: I don't think you'll need to implement your own Executor.
Instead, use something like this:

ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new ThreadOne());
service.execute(new ThreadTwo());
 
L

Let me Think

zero said:
<snipped>

Rather than hand you the solution, I suggest you have a look at the text
of the errors.


it says you need to override (ie write it yourself) method
execute(java.lang.Runnable)
You have a method
execute()
This is not the same!



This says that executer is unknown. Usually this means that you either
misspelled it, or forgot to declare it. A typical declaration is:
Object myObject = new Object();
after which you can use myObject.
A mistake a lot of newbies make is also that they forget Java is case-
sensitive. MyObject is not the same as myObject or myobject.


see above.

One more thing: I don't think you'll need to implement your own Executor.
Instead, use something like this:

ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new ThreadOne());
service.execute(new ThreadTwo());



Thanks for the Tips Zero
Your right just giving the answer is no real help in the long term.
I was also looking in the Executor API on the sun site and not the
ExecutorService

I has seen the Runnable errors and has added the run() sections in thinking
this was then i did not known about the implementation of runnable

Only one part that puzzles me now why do i need the exit command?
Thanks





------------ file ThreadPerTaskExecutor.java --------------

package test;

import java.util.concurrent.*;

class ThreadPerTaskExecutor {
// class ThreadPerTaskExecutor implements Executor {
public static void main(String args[])

{

ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new ThreadOne());
service.execute(new ThreadTwo());
System.exit(0);
}


}

------------ file ThreadTwo.java --------------
package test;


public class ThreadOne implements Runnable {

public void run() {

System.out.println("External Thread one");

}

}


------------ file ThreadTwo.java --------------


package test;

public class ThreadTwo implements Runnable {

public void run() {

System.out.println("External Thread Two");

}

}
 
Z

zero

Thanks for the Tips Zero
Your right just giving the answer is no real help in the long term.
I was also looking in the Executor API on the sun site and not the
ExecutorService

I has seen the Runnable errors and has added the run() sections in
thinking this was then i did not known about the implementation of
runnable

Only one part that puzzles me now why do i need the exit command?
Thanks





------------ file ThreadPerTaskExecutor.java --------------

package test;

import java.util.concurrent.*;

class ThreadPerTaskExecutor {
// class ThreadPerTaskExecutor implements Executor {
public static void main(String args[])

{

ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new ThreadOne());
service.execute(new ThreadTwo());
System.exit(0);
}


}

Actually, you need service.shutdown(); This tells the ExecutorService to
stop accepting new threads. When the threads it has at that point
(either running or in queue to run later) are finished, the
ExecutorService will shut down, which in your case will also shut down
the program since no more threads are running.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top