node-java icon indicating copy to clipboard operation
node-java copied to clipboard

problem: js ended faster than the java thread

Open LeungJacky opened this issue 6 years ago • 0 comments

I try to run a jar that used thread in my java code Here is the excepted result(the jar work fine when using java -jar thread.jar)

here is Main
here is Main 8
here is TestThread
try
here is looping: 0
here is looping: 1
here is looping: 2
here is looping: 3
here is looping: 4
here is looping: 5
here is looping: 6
here is looping: 7
here is looping: 8
here is looping: 9
here is TestThread after sleep

but the node js program ended very fast

i only got this result
here is Main
here is Main 8
here is TestThread
try
here is looping: 0
ended

Source code of js program

var java = require("java");

java.classpath.push("thread.jar");

var instance = java.newInstanceSync("Main");

java.callStaticMethodSync("Main", "main", ["testing"])
console.log('ended')

Source code of java program

image

import threading.TestThread;
public class Main{
    public static void main(String [] args){
    	System.out.println("here is Main");

        Thread tt = new Thread(new TestThread());
        tt.start();
    	System.out.println("here is Main 8");

    }
}
package threading;
public class TestThread implements Runnable{
	@Override
	public void run()  {
		System.out.println("here is TestThread");
        try{
            System.out.println("try");
            int i=0;
            while(i< 10){
                System.out.println("here is looping: "+i);
                Thread.sleep(5000);
                i++;
            }
        }catch(Exception e){
		    System.out.println("exception on sleep");
        }
		System.out.println("here is TestThread after sleep");
	}
}

LeungJacky avatar Dec 26 '19 06:12 LeungJacky