Guest User

Untitled

a guest
Jan 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public static void main(String[] args) {
  2. SingcomUC1SerialRunnableThreadClass t1 = new SingcomUC1SerialRunnableThreadClass("READ_THREAD");
  3. t1.start1();
  4. SingcomUC1SerialRunnableThreadClass t2 = new SingcomUC1SerialRunnableThreadClass("Write_THREAD");
  5. t2.start1();
  6. System.out.println("active thread count=" + Thread.activeCount());
  7. }
  8.  
  9. Thread t;
  10. String threadName;
  11.  
  12. public SingcomUC1SerialRunnableThreadClass(String name) {
  13. threadName = name;
  14. }
  15.  
  16. public void start1() {
  17. if (t == null) {
  18. t = new Thread(this, threadName);
  19. t.start();
  20. }
  21. }
  22.  
  23. @Override
  24. public void run() {
  25. System.out.println("run() invokations");
  26. while (true) {
  27. if (threadName.equalsIgnoreCase(singcomUC1Readthread)) {
  28. System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
  29. try {
  30. commonSection();
  31. } catch (InterruptedException ex) {
  32. Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
  33. }
  34. try {
  35. Thread.sleep(1000);
  36. } catch (InterruptedException ex) {
  37. Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
  38. }
  39. }
  40. if (threadName.equalsIgnoreCase(singcomUC1Writethread)) {
  41. System.out.println(Thread.currentThread().getName() + "--" + Thread.activeCount());
  42. try {
  43. commonSection();
  44. Thread.sleep(300);
  45. } catch (InterruptedException ex) {
  46. Logger.getLogger(SingcomUC1SerialRunnableThreadClass.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48. }
  49. }
  50. }
  51.  
  52. public void commonSection() throws InterruptedException {
  53. InsertValuesIntoUC1DBtable.insertDefaultValueAndStatusIntoUC1DBtable(singcomUC1DeviceId);
  54. ReadOrUpdateUC1CONTROLtable.insertDefaultValueAndStatusIntoDC1DBtable(singcomUC1DeviceId);
  55. }
  56.  
  57. public static void insertDefaultValueAndStatusIntoUC1DBtable(int deviceid) {
  58. try {
  59. Connection con = null;
  60. DatabaseConnection databaseConnection_obj = new DatabaseConnection();
  61. con = databaseConnection_obj.getConnection();
  62. PreparedStatement pst = con.prepareStatement("select * from uc1db");
  63. ResultSet rs = pst.executeQuery();
  64. if (!rs.next()) {
  65. pst = con.prepareStatement("insert into uc1db(deviceid,frequency,attenuation,transmitterstatus,devicestatus) values(?,?,?,?,?)");
  66. pst.setInt(1, deviceid);
  67. pst.setString(2, "0");
  68. pst.setString(3, "0");
  69. pst.setString(4, "0");
  70. pst.setString(5, "online");
  71. pst.executeUpdate();
  72. }
  73. rs.close();
  74. pst.close();
  75. con.close();
  76. } //**
  77. catch (SQLException ex) {
  78. Logger.getLogger(InsertValuesIntoUC1DBtable.class.getName()).log(Level.SEVERE, null, ex);
  79. }
  80. }
  81.  
  82. public Connection getConnection() {
  83. Connection con = null;
  84. try {
  85. Class.forName("com.mysql.jdbc.Driver");
  86. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
  87. } catch (Exception e) {
  88. System.out.println(" Db Exception " + e);
  89. }
  90. return con;
  91. }
  92.  
  93. output:
  94. active thread count=3
  95. run() invokations
  96. run() invokations
  97. singcomUC1ReadThread--2
  98. singcomUC1WriteThread--2
  99. singcomUC1WriteThread--4
  100. singcomUC1WriteThread--4
Add Comment
Please, Sign In to add comment