SQL/MX Programming Manual for Java

Sample Programs
HP NonStop SQL/MX Programming Manual for Java523726-003
C-16
MultiThread2.sqlj—Threads With Explicit Connection
Contexts
MultiThread2.sqlj—Threads With Explicit
Connection Contexts
The MultiThread2 program updates the price of a specified part in separate threads. To
ensure the synchronization of the threads, the program uses explicit connection
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-4. MultiThread2.sqlj—Threads With Different Connection
Contexts (page1of2)
import java.sql.*;
import java.math.*;
import sqlj.runtime.*;
//Connection context class declaration
#sql context SQLMXCtx;
public class MultiThread2 extends Thread {
// Iterator and variable declarations
#sql static iterator PartsPricesIter2 (BigDecimal partnum,
double price);
BigDecimal partNum;
double percent;
//Class constructor
MultiThread2 (BigDecimal partNum, double percent) {
this.partNum = partNum;
this.percent = percent;
}
//main() method
public static void main (String [] args) {
try {
#sql { MODULE samdbcat.sales.mt2mod };
showPrices();
//Instantiating Thread objects
MultiThread2 t1 = new MultiThread2(new BigDecimal(186),
(double)15);
MultiThread2 t2 = new MultiThread2(new BigDecimal(186),
(double)2);
//Starting the execution of the threads
t1.start();
t2.start();
t1.join();
t2.join();
showPrices();
}