Creating and using java threads in javascript - in Nashorn Javascript (js) Engine in Java 8




Program/ Example 1 of Nashorn Javascript (js) Engine in Java 8 - Creating and using java threads in javascript >

package nashorn5_thread;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Creating and using java threads in javascript.
*
*/
public class Nashorn_useJavaObjects_Thread_InJavascript {
   public static void main(String[] args) throws Exception {
       ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       System.out.println("In java - START");
       //Call js (javascript) file
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");
      
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornScript.js
Step 1 - Declare java Runnable interface type in javascript
Step 2 - Declare java THREAD in javascript
Step 3 - Current thread name = main
Step 4 - Create object of Runnable (i.e. myJavaRunnable)
Step 5.1 - Create Thread1 on myJavaRunnable object
x1 - Current thread name = Thread-1
Step 5.2 - Create Thread2 on myJavaRunnable object
x1 - Current thread name = Thread-2
End myNashornScript.js
In java - FINISH
*/
Note : Output may vary on different machines in different executions because thread behavior is unpredictable in java.

E:/pocJmse/myNashornJavaScript.js looks like this (see java code equivalent to this javascript file below) >
print('In myNashornScript.js');
print('Step 1 - Declare java Runnable interface type in javascript');
var javaRunnable = Java.type('java.lang.Runnable');
print('Step 2 - Declare java THREAD in javascript');
var javaThread = Java.type('java.lang.Thread');
print('Step 3 - Current thread name = ' + javaThread.currentThread().getName());
print('Step 4 - Create object of Runnable (i.e. myJavaRunnable) ');
var myJavaRunnable = Java.extend(javaRunnable, {
   // Provide implementation of run method
   run : function() {
          print('x1 - Current thread name = '
                       + javaThread.currentThread().getName());
   }
});
print('Step 5.1 - Create Thread1 on myJavaRunnable object');
var myThread1 = new javaThread(new myJavaRunnable());
myThread1.start();
print('Step 5.2 - Create Thread2 on myJavaRunnable object');
var myThread2 = new javaThread(new myJavaRunnable());
myThread2.start();
print('\nEnd myNashornScript.js');

Let's write java code equivalent to above javascript file >
public class MyClass {
   public static void main(String args[]) {
          //Same as 'Step 4.1 - Create object of Runnable (i.e. myJavaRunnable) '
          Runnable myJavaRunnable = new Runnable() {
               @Override
               public void run() {
                System.out.println("x1 - Current thread name = "+Thread.currentThread().getName());
               }
          };
          //Same as 'Step 4.2 - Create Thread1 on myJavaRunnable object'
          Thread myThread1 = new Thread(myJavaRunnable);
          myThread1.start();
         
          //Same as 'Step 4.3 - Create Thread1 on myJavaRunnable object'
          Thread myThread2 = new Thread(myJavaRunnable);
          myThread2.start();
         
         
   }
}
/* OUTPUT
x1 - Current thread name = Thread-0
x1 - Current thread name = Thread-1
*/






Program/ Example 2 of Nashorn Javascript (js) Engine in Java 8 - Creating and using java threads in javascript >

package nashorn5_thread2;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Creating and using java threads in javascript.
*
*/
public class Nashorn_useJavaObjects_Thread_InJavascript {
   public static void main(String[] args) throws Exception {
       ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       System.out.println("In java - START");
       //Call js (javascript) file
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");
      
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornScript.js
Step 1 - Declare java THREAD in javascript
Step 2 - Current thread name = main
Step 3 - Create and start javaThread in single line
x1 - Current thread name = Thread-1
End myNashornScript.js
In java - FINISH
*/


E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornScript.js');
print('Step 1 - Declare java THREAD in javascript');
var javaThread = Java.type('java.lang.Thread');
print('Step 2 - Current thread name = ' + javaThread.currentThread().getName());
print('Step 3 - Create and start javaThread in single line');
new javaThread(
          function() {
                 print('x1 - Current thread name = '
                              + javaThread.currentThread().getName());
          }).start();
print('\nEnd myNashornScript.js');



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :