Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- SingcomUC1SerialRunnableThreadClass t1 = new SingcomUC1SerialRunnableThreadClass("READ_THREAD");
- t1.start1();
- SingcomUC1SerialRunnableThreadClass t2 = new SingcomUC1SerialRunnableThreadClass("Write_THREAD");
- t2.start1();
- System.out.println("active thread count=" + Thread.activeCount());
- }
- Thread t;
- String threadName;
- public SingcomUC1SerialRunnableThreadClass(String name) {
- threadName = name;
- }
- public void start1() {
- if (t == null) {
- t = new Thread(this, threadName);
- t.start();
- }
- }
- @Override
- public void run() {
- System.out.println("run() invokations");
- while (true) {
- if (threadName.equalsIgnoreCase(singcomUC1Readthread)) {
- System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
- try {
- commonSection();
- } catch (InterruptedException ex) {
- Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException ex) {
- Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- if (threadName.equalsIgnoreCase(singcomUC1Writethread)) {
- System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
- try {
- commonSection();
- Thread.sleep(300);
- } catch (InterruptedException ex) {
- Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }
- public void commonSection() throws InterruptedException {
- InsertValuesIntoUC1DBtable.insertDefaultValueAndStatusIntoUC1DBtable(singcomUC1DeviceId);
- ReadOrUpdateUC1CONTROLtable.insertDefaultValueAndStatusIntoDC1DBtable(singcomUC1DeviceId);
- }
- public static void insertDefaultValueAndStatusIntoUC1DBtable(int deviceid) {
- try {
- Connection con = null;
- DatabaseConnection databaseConnection_obj = new DatabaseConnection();
- con = databaseConnection_obj.getConnection();
- PreparedStatement pst = con.prepareStatement("select * from uc1db");
- ResultSet rs = pst.executeQuery();
- if (!rs.next()) {
- pst = con.prepareStatement("insert into uc1db(deviceid,frequency,attenuation,transmitterstatus,devicestatus) values(?,?,?,?,?)");
- pst.setInt(1, deviceid);
- pst.setString(2, "0");
- pst.setString(3, "0");
- pst.setString(4, "0");
- pst.setString(5, "online");
- pst.executeUpdate();
- }
- rs.close();
- pst.close();
- con.close();
- } //**
- catch (SQLException ex) {
- Logger.getLogger(InsertValuesIntoUC1DBtable.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public Connection getConnection() {
- Connection con = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
- } catch (Exception e) {
- System.out.println(" Db Exception " + e);
- }
- return con;
- }
- output:
- active thread count=3
- run() invokations
- run() invokations
- singcomUC1ReadThread--2
- singcomUC1WriteThread--2
- singcomUC1WriteThread--4
- singcomUC1WriteThread--4
Add Comment
Please, Sign In to add comment