Hi:
I'm
trying to use two java threads t1 and t2, and each one uses exec() to start the
same C native application. It seems JVM (BlackDown 1.1.8 v1) never
switches from t1 to t2. The output looks like:
In
thread t1: i = 0
In
thread t1: i = 1
In
thread t1: i = 2
...
I put
suspend() inside method run(), but it couldn't stop t1 from running. What
I missed?
The
attached C and java codes are real and can be compiled.
Thank
you.
Lee
---------------------------------------------------------------------------
/*******************************************************
* C application source code
********************************************************/
* C application source code
********************************************************/
#include <stdio.h>
int main(int argc, char*
argv[])
{
int i;
for(i=0; i<1000; i++)
{
printf("In thread %s: i = %d\n", argv[1], i);
fflush(stdout);
sleep(1);
}
{
int i;
for(i=0; i<1000; i++)
{
printf("In thread %s: i = %d\n", argv[1], i);
fflush(stdout);
sleep(1);
}
return
0;
}
}
=======================================
/*******************************************************
* java application source code
********************************************************/
import java.lang.*;
import java.io.*;
* java application source code
********************************************************/
import java.lang.*;
import java.io.*;
class communicate
{
public static void main(String args[])
{
myThread t1 = new myThread("t1");
myThread t2 = new myThread("t2");
{
public static void main(String args[])
{
myThread t1 = new myThread("t1");
myThread t2 = new myThread("t2");
t1.run();
t2.run();
}
}
t2.run();
}
}
class myThread extends
Thread
{
String szThreadName;
{
String szThreadName;
myThread(String
szName)
{
super(szName);
szThreadName = szName;
}
{
super(szName);
szThreadName = szName;
}
public void
run()
{
Process proc_;
int rc;
char[] buf = new char[1024];
File fileObj = new File("a.out");
String fileName = fileObj.getAbsolutePath();
String cmd[] = new String[2];
cmd[0] = fileName;
cmd[1] = szThreadName;
{
Process proc_;
int rc;
char[] buf = new char[1024];
File fileObj = new File("a.out");
String fileName = fileObj.getAbsolutePath();
String cmd[] = new String[2];
cmd[0] = fileName;
cmd[1] = szThreadName;
try
{
System.out.println("the executable name is:"+fileName);
proc_ = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(proc_.getInputStream()));
{
System.out.println("the executable name is:"+fileName);
proc_ = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(proc_.getInputStream()));
do
{
rc = in.read(buf);
if(rc>0)
System.out.print(buf);
suspend(); // why this suspend() doesn't work???
}while(rc != -1);
}
catch(IOException e)
{
System.out.println("got e after try exec()");
e.printStackTrace();
}
} // end of run()
} // end of myThread class