Splitting processor performance for multiple processes in Java

I

Icarus

I am currently working on creating automatic players for a game, using
Java and alpha beta search. For this, I need to evenly distribute the
processor performance between all automatic players.

I am not really familiar with threads and would like to know if there
is a simple way of managing this. The solution itself shouldn't
require a lot of processor work, as I need everything I can get for
the program itself.

I would be very glad if someone could explain or point out some
methods that would be useful. At the moment, I don't really know where
to look myself.
 
L

Lew

Icarus said:
I am currently working on creating automatic players for a game, using
Java and alpha beta search. For this, I need to evenly distribute the
processor performance between all automatic players.

I am not really familiar with threads and would like to know if there
is a simple way of managing this. The solution itself shouldn't
require a lot of processor work, as I need everything I can get for
the program itself.

I would be very glad if someone could explain or point out some
methods that would be useful. At the moment, I don't really know where
to look myself.

Multi-threaded programming is not really simple. Read /Java Concurrency in
Practice/ by Brian Goetz, et al.
 
M

Mark Space

Icarus said:
I am currently working on creating automatic players for a game, using
Java and alpha beta search. For this, I need to evenly distribute the
processor performance between all automatic players.

I am not really familiar with threads and would like to know if there
is a simple way of managing this. The solution itself shouldn't

As Lew said, no, there just isn't a simple way of doing this. I second
his recommendation of Java Concurrency in Practice.

Here's a link to concurrency in Java to get you started:
<http://java.sun.com/docs/books/tutorial/essential/concurrency/>

Here's a decent description of the Java Memory Model, which is critical
to understand if you do any work with threads:

<http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html>

Here's a link to Executors, which are your best bet for making things
simple, I think. For your search algorithm, you'll have to return a
result from your worker threads, so it's super important to understand
the Java Memory Model or none of this is going to make a lick of sense
to you:

<http://java.sun.com/docs/books/tutorial/essential/concurrency/exinter.html>

You'll need to understand Callable and Future well to return a result.
For example, let's say you define some result of your search:

class MyResult {
// ???
}

OK, now you need to return that result from a thread:

class MyTask implements Callable<MyResult> {
public MyResult call() throws Exception {
MyResult result = null;
// do stuff, then:
return result;
}
}

Finally, you need return this result to a "mother thread" that is in
charge of the various sub-threads. I just busy wait here, that's
probably not the best, but gives you the idea.

class ExecutorTest {

public static void test()
throws InterruptedException,
ExecutionException {
ExecutorService es = Executors.newCachedThreadPool();
MyTask myTask = new MyTask();
Future<MyResult> synchResult = es.submit( myTask );
while( !synchResult.isDone() ) {
Thread.sleep(1000); // wait a bit
}
MyResult atLast = synchResult.get();
//...
}
}

Note: syntax checked, not executed. This is safe, I believe, and gives
you an idea how the basic classes work together. More sophisticated
examples might require careful design to keep them as safe as these
classes from the API.

Good luck, and I think we'd all be interested in seeing what you devise
for yourself in the longer run.
 
L

Lew

Mark said:
class MyTask implements Callable<MyResult> {
public MyResult call() throws Exception {
MyResult result = null;
// do stuff, then:
return result;
}
}

A nit: initializing a local variable to 'null' is not always optimal, though
it is sometimes necessary.
 
M

Mark Space

Lew said:
A nit: initializing a local variable to 'null' is not always optimal,
though it is sometimes necessary.

Since I did compile this example, I just did that to get rid of the
compiler warning. I used a local variable because, for someone
unfamiliar with Callable, I thought declaring a local variable, then on
the next line putting the "do stuff" comment, and then finally returning
the local was less confusing than say:

class MyTask implements Callable<MyResult> {
public MyResult call() throws Exception {
// do stuff, then:
return new MyResult();
}
}

Which is cleaner but perhaps less clear how the result should be derived.

Another nit: "throws Exception" got added by the auto-code generation.
In this example it's of course not needed.
 
R

Roedy Green

I am not really familiar with threads and would like to know if there
is a simple way of managing this. The solution itself shouldn't
require a lot of processor work, as I need everything I can get for
the program itself.

There is nothing special you have to do. If you have multiple
threads, the OS will automatically distribute the tasks over all the
CPUs it has available in some efficient way. A task may run on a
different CPU at different stages of its life.
--
Roedy Green Canadian Mind Products
http://mindprod.com
"Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
whose ultimate consequences could be second only to global nuclear war."
~ Environment Canada (The Canadian equivalent of the EPA on global warming)
 
I

Icarus

I'm very sorry for not replying. Somehow my mail notification did not
work, and hence I thought that I did not get any replies at all. Thank
you for the replies, I will now dive into what you've pointed out.
 
L

Lew

Icarus said:
I'm very sorry for not replying. Somehow my mail notification did not
work, and hence I thought that I did not get any replies at all. Thank
you for the replies, I will now dive into what you've pointed out.

What newsgroup client do you use? Mine (Thunderbird) and others of which I've
heard do not notify one for newsgroup posts.
 
I

Icarus

What newsgroup client do you use?  Mine (Thunderbird) and others of which I've
heard do not notify one for newsgroup posts.

Actually, I was thinking that there would be a message sent to my
given mail adress, much like it's done on usual boards. I got this
working once, however this has been ages ago and I don't really recall
what I did. I'm using regular Outlook for mail and access the
newsgroup with Firefox.


Icarus
 
M

Mark Space

Icarus said:
Actually, I was thinking that there would be a message sent to my
given mail adress, much like it's done on usual boards. I got this
working once, however this has been ages ago and I don't really recall
what I did. I'm using regular Outlook for mail and access the
newsgroup with Firefox.

By "Firefox" do you mean Google Groups?

Google Groups is just a front end for the NNTP protocol. Most mail
readers have this built in. However, that's totally different from
getting email notification from GG. Email notification only works when
the group is a mailing list. This is rather different.
 
M

Martin Gregorie

Google Groups is just a front end for the NNTP protocol. Most mail
readers have this built in. However, that's totally different from
getting email notification from GG. Email notification only works when
the group is a mailing list. This is rather different.

Rather than using GG or OE to read USENET groups, try a real newsreader.

IME a news reader is much faster and more convenient than using a
browser to read newsgroups via GG. Besides, the good ones provide a
killfile facility to filter out people and threads you'd rather not see.

Using a mail client is better, but they too can lack a decent killfile
and anyway they aren't optimised for news reading.

IMO Forte's Agent (or FreeAgent) is among the best Windows newsreaders.
FreeAgent's only disadvantage is the lack of kill file. However if you
like Free Agent you can just pony up $29, register it and there's the
killfile. Sadly, a killfile seems to be a necessity on c.l.j.p

Forte Inc's site is: http://www.forteinc.com
 
I

Icarus

That looks interesting, however it seems free Agent is no longer
worked on. Do you by chance know a free reader with kill file? If
nothing comes to mind, I'll just have to search for myself.
 
L

Lars Enderin

Icarus said:
That looks interesting, however it seems free Agent is no longer
worked on. Do you by chance know a free reader with kill file? If
nothing comes to mind, I'll just have to search for myself.

Thunderbird has a kind of killfile, but you can only filter on subject
and/or sender contents. I think you need something like SpamAssassin.
 
R

RedGrittyBrick

Lars said:
Thunderbird has a kind of killfile, but you can only filter on subject
and/or sender contents. I think you need something like SpamAssassin.

I have Thunderbird reading news through NewsProxy. It works well enough
for me.

NewsProxy is very easy to set up. It is derived from NFilter. Neither
appear to be actively maintained.

ftp://ftp.zedz.net/incoming/NewsProxy_1.2.4.txt
ftp://ftp.zedz.net/incoming/NewsProxy_1.2.4.zip (binary & source)
http://nfilter.org/ (docs)
 
R

RedGrittyBrick

Tim said:
Doesn't Thunderbird (www.mozilla.org/thunderbird) have a killfile?

Thunderbird has "Filters" but they only operate on the Subject and From
headers. This is adequate for almost all nuisances. Occasionally you
need to filter by other headers, such as the various headers some
news-servers provide in response to an NNTP XOVER request.
 
J

Joshua Cranmer

RedGrittyBrick said:
Thunderbird has "Filters" but they only operate on the Subject and From
headers. This is adequate for almost all nuisances. Occasionally you
need to filter by other headers, such as the various headers some
news-servers provide in response to an NNTP XOVER request.

Not for long, though: news filters will be able to filter on any header
in TB 3.0.
 
M

Martin Gregorie

That looks interesting, however it seems free Agent is no longer worked
on.
Huh? Their site says that 5.0 beta was released to testing on 05Dec08.

I'd give it a look anyway, since you can trial it for free. I like its
killfile implementation a lot. The ability to define a new kill rule and
immediately apply it to the local news database is really good. It has by
far the most flexible approach to managing the local news database that
I've seen - you can set detailed expiry rules per newsgroup or globally
and then override that by locking individual articles against expiry.
Do you by chance know a free reader with kill file?
You could try XPN. It has both scoring rules (which lets you apply a
'death of a thousand cuts' approach to killfiling - similar to the way
Spamassassin works) and action rules (such as 'kill', which work directly
on a post as well as on the accumulated score).

I'm a Linux user so I'm pretty much out of touch with Windows
newsreaders. I've tried Thunderbird, which is OK-ish, but am now using
Pan (Linux-only), which is better. I have Agent and XPN bookmarked in
case I decide I want something better than Pan.

Thunderbird and XPN run natively on Linux and Windows. Agent can be run
on Linux with WINE, which provides a Windows run-time environment for
Windows apps.
 
I

Icarus

Thank you all for the information. I will start trying Thunderbird, as
that keeps me from having to install Python. If that's not
satisfactory, I'll switch to XPN.

Regarding the original problem: I have postponed the problem for
later, as I don't need the whole functionality from the start, and I
need to get some code out now. I'm pretty positive that I will be
adding some Executor-based approach later on.
 
J

John W Kennedy

Thunderbird has a kind of killfile, but you can only filter on subject
and/or sender contents. I think you need something like SpamAssassin.

Thunderbird 3.0, now in beta, allows you to filter on any header.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top