SQL/MX Programming Manual for Java
Sample Programs
HP NonStop SQL/MX Programming Manual for Java—523726-003
C-13
MultiThread1.sqlj—Threads With Explicit Execution
Contexts
MultiThread1.sqlj—Threads With Explicit
Execution Contexts
The MultiThread1 program updates the price of a specified part in separate threads. To
ensure the synchronization of the threads, the program uses explicit execution
contexts. For more information, see Multithreading on page 3-23.
This SQLJ program uses tables in the SQL/MX sample database. To install the sample
database, see the SQL/MX Quick Start.
SQLJ Source File
Example C-3. MultiThread1.sqlj—Threads With Explicit Execution
Contexts (page1of2)
import java.sql.*;
import java.math.*;
import sqlj.runtime.*;
public class MultiThread1 implements Runnable {
//Iterator and variable declarations
#sql static iterator PartsPricesIter1 (BigDecimal partnum,
double price);
BigDecimal partNum;
double percent;
//Class constructor
MultiThread1 (BigDecimal partNum, double percent) {
this.partNum = partNum;
this.percent = percent;
}
//main() method
public static void main (String [] args) {
try {
#sql { MODULE samdbcat.sales.mt1mod };
showPrices();
//Instantiating Runnable objects
MultiThread1 mt1 = new MultiThread1(new BigDecimal(186),
(double)15);
MultiThread1 mt2 = new MultiThread1(new BigDecimal(186),
(double)2);
//Wrapping the Runnable objects in Thread objects
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
//Starting the execution of the threads
t1.start();
t2.start();
t1.join();
t2.join();
showPrices();
}