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 / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

instructions for using green threads with blackdown java?

Thread view: 
dtliu_04@hotmail.com - 09 Dec 2004 08:38 GMT
Hi,

I can't seem to find working  directions for running green threads with
java. I have a sample program which should run one thread with higher
priority than another, but it doesn't seem to work that way when I run
it. Any clues?

My goal is to prioritize one thread over another. I downloaded and
installed blackdown, compiled the code and ran it. it doesn't seem to
work

When I try running blackdown with '-green', I get an error:

> java -green TestThreads 1 1 1 10
Error: green VM not supported

When I run by setting THREADS_FLAG=green, the priorities don't work:

> javac TestThreads.java && THREADS_FLAG=green  java TestThreads 1 1 1
10
MAX_PRIORITY=10
MIN_PRIORITY=1
NORM_PRIORITY=5
Starting thread B0
priority=10
Starting thread A0
priority=1
---

I do an 'ls' on the output files. If one thread runs with higher
priority than another, then, file outB should be much larger than outA,
but this is not the case:

-rw-rw-r--  1 dtliu dtliu 188510 Dec  9 01:33 outB
-rw-rw-r--  1 dtliu dtliu 186572 Dec  9 01:33 outA

---

Here is the program, just in case I'm doing something wrong:

import java.io.*;
import java.util.*;

class TestThreads{

public static void main(String []args){
int numA = Integer.parseInt(args[0]);
int numB = Integer.parseInt(args[1]);
int priorityA = Integer.parseInt(args[2]);
int priorityB = Integer.parseInt(args[3]);

System.out.println("MAX_PRIORITY=" + Thread.MAX_PRIORITY );
System.out.println("MIN_PRIORITY=" + Thread.MIN_PRIORITY );
System.out.println("NORM_PRIORITY=" + Thread.NORM_PRIORITY );

try{
PrintWriter pwA = new PrintWriter(new FileWriter("outA"));
PrintWriter pwB = new PrintWriter(new FileWriter("outB"));

createThreads(numB, priorityB, "B",pwB);
createThreads(numA, priorityA, "A",pwA);

}catch(Exception E){
System.out.println("Problem");
System.exit(1);
}

} //main

public static void createThreads(int numThreads, int priority, String
prefix, PrintWriter pw){
for(int i=0; i<numThreads; i++){
String name = prefix + i;
System.out.println("Starting thread " + name);
Thread t = new Thread(new ThreadA(pw, name));
t.setPriority(priority);
System.out.println("priority=" + t.getPriority());
t.start();
//   System.out.println("Afterwards"); //ddd
} //for
} //createThreads

} //TestThreads

class ThreadA implements Runnable{

ThreadA(PrintWriter bw, String threadClass){
this.bw = bw;
this.threadClass = threadClass;
}

private String threadClass = null;
private PrintWriter bw = null;
private int counter = 0;

public void run(){
while(true){
// try{ Thread.sleep(100); }catch(Exception e){
System.out.println("thread " + threadClass + " interrupted."); }
for(int i=0; i<100000; i++){
int x = 2 / (i + 1);
} //for

Date d = new Date(System.currentTimeMillis());
bw.println(threadClass + " " + counter++ + ": " + d);
bw.flush();
 
 } //while
} //run

} //ThreadA
Brett Foster - 09 Dec 2004 08:59 GMT
**snip**

>>javac TestThreads.java && THREADS_FLAG=green  java TestThreads 1 1 1
>
[quoted text clipped - 16 lines]
>
> ---

*snip*

Thread priority is cpu-bound. The operations you are preforming are I/O
bound. I/O is much slower than normal calculations. Because of the magic
of an OS, the threads spent 99% of the time waiting for the operation to
complete (blocking).

Try a more calculation intensive operation with no I/O.

Brett
dtliu_04@hotmail.com - 09 Dec 2004 09:33 GMT
I tried your suggestion, by changing "100000" (100 thousand) to
"100000000" (10 million). These 10^8 divides take 12 full seconds, so
the process is definitely not IO bound. Same result though, the
high-priority thread is not moving any quicker than the low-priority
thread. I really don't think that green threads are running. Any other
suggestions? Is there a recipe for testing out whether green threading
is working?

TIA
Brett Foster - 09 Dec 2004 10:08 GMT
> I tried your suggestion, by changing "100000" (100 thousand) to
> "100000000" (10 million). These 10^8 divides take 12 full seconds, so
> the process is definitely not IO bound. Same result though, the

Oh sorry I missed that in the original.

> high-priority thread is not moving any quicker than the low-priority
> thread. I really don't think that green threads are running. Any other
> suggestions? Is there a recipe for testing out whether green threading
> is working?

Hmm... Ok I've been reading about green threads. I see why that other
file should be much larger.

"To share the CPU with a lower-priority thread, a thread can call
Thread.sleep() or, more precisely, call wait() on an object until the
low-priority thread signals the end of its run by calling notify() on
that object. "

http://www.javaworld.com/javaworld/javaqa/1999-10/02-qa-native.html

So I'm guessing it's not running? -- No maybe not -- The two threads
could be playing "tag team" on the i/o blocking. High priority starts,
then blocks, letting the low priority start where low priority yields
only at the i/o blocking (just like the high priority thread). Low ->
High -> Low -> High -> Low... Basically we're back to blocking because
the only place the thread can be interrupted is at that point.

Of course this depends on some properties of green threads that I don't
know enough about. In particular that IO blocking yields to other
threads for your OS/jvm and that it allows a lower priority to continue.

Brett

> TIA
dtliu_04@hotmail.com - 09 Dec 2004 10:37 GMT
Hey Brett,

Thanks for your suggestions. I don't think this is it though. The high
prio thread should preempt the low prio one. From the same source that
you cite: "The only promise green threads make is that a high-priority
thread will preempt a low-priority one."

I just wish that someone had a sample program that illustrates green
threads in blackdown handling priority correctly...then I would know
for sure whether green threads was working on my system or not.

David
dtliu_04@hotmail.com - 09 Dec 2004 10:54 GMT
I also notice that in my blackdown distro, I'm missing the
green_threads directory. When I do a 'find' for 'green_threads', I
dont' find anything:

dtliu@vega:j2sdk1.4.2$ pwd
/usr/java/blackdown/j2sdk1.4.2
dtliu@vega:j2sdk1.4.2$ find . -iname 'green_threads'
dtliu@vega:j2sdk1.4.2$

Other people seem to refer to:

$JAVA_HOME/jre/lib/i386/green_threads/libhpi.so
Is this a version problem?

I used: j2sdk-1.4.2-01-linux-i586.bin

TIA
Chris Smith - 09 Dec 2004 18:36 GMT
> I also notice that in my blackdown distro, I'm missing the
> green_threads directory. When I do a 'find' for 'green_threads', I
> dont' find anything:

> I used: j2sdk-1.4.2-01-linux-i586.bin

You may be reading old documentation.  No one has seriously discussed
using green threads for half a decade now, and it may be that the green
threads implementation has been dropped from modern versions of the
Blackdown VM.

Just a guess, though.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Brett Foster - 09 Dec 2004 11:16 GMT
> Hey Brett,
>
> Thanks for your suggestions. I don't think this is it though. The high
> prio thread should preempt the low prio one. From the same source that
> you cite: "The only promise green threads make is that a high-priority
> thread will preempt a low-priority one."

Yeah, that's true, but only at a context switch which does not
_nessesarily_ happen when that thread becomes available again after the
IO operation stops blocking. (Note emphasis on nessesarily.)

The real problem, I guess, is that that all of this is abstracted from
you. Without knowledge of how green threads are implemented at a low
level, you won't know it interrupts other threads (i.e. is it
co-operative or preemptive or whatever). And for that matter, it isn't
very portable (cross platforms, cross versions) if you knew those details.

Anyways I hope somebody who knows more about threads comments... Maybe
we're looking at all the wrong things here, eh?

> I just wish that someone had a sample program that illustrates green
> threads in blackdown handling priority correctly...then I would know
> for sure whether green threads was working on my system or not.
>
> David
Brett Foster - 09 Dec 2004 11:18 GMT
>> Hey Brett,
>>
[quoted text clipped - 10 lines]
> you. Without knowledge of how green threads are implemented at a low
> level, you won't know it interrupts other threads (i.e. is it

*sigh*
you won't know when it interrupts...

> co-operative or preemptive or whatever). And for that matter, it isn't
> very portable (cross platforms, cross versions) if you knew those details.
[quoted text clipped - 7 lines]
>>
>> David


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



©2008 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.