Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

java.util.concurrent

Thread view: 
Let me Think - 29 Oct 2005 10:35 GMT
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");

}

}
zero - 29 Oct 2005 12:49 GMT
> 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());
Let me Think - 29 Oct 2005 15:17 GMT
>> Dear All
>> I'm fairly new to Java and have been attempting to get the
[quoted text clipped - 43 lines]
> 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");

}

}
zero - 29 Oct 2005 19:38 GMT
> Thanks for the Tips Zero
> Your right just giving the answer is no real help in the long term.
[quoted text clipped - 27 lines]
>
> }

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.
Roedy Green - 29 Oct 2005 15:47 GMT
On Sat, 29 Oct 2005 10:35:53 +0100, "Let me Think"
<letmethink@unknown.com> wrote, quoted or indirectly quoted someone
who said :

>Any ideas what I have done wrong

Yes. You asked us to diagnose a program without showing  it to us.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.