{"id":45,"date":"2009-04-15T18:55:04","date_gmt":"2009-04-15T23:55:04","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=45"},"modified":"2011-08-05T21:36:53","modified_gmt":"2011-08-06T02:36:53","slug":"java-lock-class-is-great-for-test-casing","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2009\/04\/15\/java-lock-class-is-great-for-test-casing\/","title":{"rendered":"Java Lock class is great for test casing"},"content":{"rendered":"<p>OK, so while trying to understand Oracle&#8217;s redo latching mechanisms (we have been experiencing a nightmare with log file sync since switching to a new DMX subsystem), I found that java 1.5 has a new Lock class. How cool is that?!!!<\/p>\n<pre lang=\"java\" line=\"1\">\r\nimport java.io.*;\r\nimport java.util.concurrent.locks.*;\r\nimport java.net.*;\r\nimport java.util.*;\r\n\r\nclass MyThread implements Runnable {\r\n  static Lock l = new ReentrantLock();\r\n  static Lock lCopy = new ReentrantLock();\r\n  static Lock lAllocation = new ReentrantLock();\r\n  Thread thrd;\r\n  static String logBuff[] = new String[10];\r\n  static PrintStream f;\r\n\r\n  MyThread(int name){\r\n    if (f == null) {\r\n      try {\r\n        f = new PrintStream(new FileOutputStream(\"\/tmp\/log.log\"),true);\r\n      }\r\n      catch(Exception e) {\r\n        System.out.println(\"Log file does not exist\");\r\n        System.exit(1);\r\n      }\r\n    }\r\n    thrd = new Thread(this, name + \"\");\r\n    thrd.start(); \/\/ start the thread\r\n  }\r\n\r\n  private void redoAllocation(String buffVal) {\r\n    for (int i = 0; i < 10; i++) {\r\n      if (logBuff[i] == null) {\r\n        System.out.println(i + \" should be \" + buffVal);\r\n        logBuff[i] = buffVal;\r\n      }\r\n    }\r\n  }\r\n\r\n  private void redoWrite() {\r\n\r\n  }\r\n\r\n  private void writeRedo() {\r\n    try {\r\n      Thread.sleep(3000);\r\n      lCopy.lock();\r\n      System.out.println(\"Got write lock\");\r\n      for (int i = 0; i < 10; i++) {\r\n        if (logBuff[i] == null) {\r\n          f.println(i + \":\" + logBuff[i]);\r\n        }\r\n      }\r\n      lCopy.unlock();\r\n    }\r\n    catch(Exception e) {\r\n      System.out.println(e);\r\n    }\r\n  }\r\n\r\n  private void redoCopy(String buffVal) {\r\n    lCopy.lock();\r\n    System.out.println(\"Got copy lock\");\r\n    redoAllocation(buffVal);\r\n    System.out.println(buffVal);\r\n    lCopy.unlock();\r\n    writeRedo();\r\n  }\r\n\r\n  public void run(){\r\n    System.out.println(thrd.getName() + \" starting.\");\r\n    try {\r\n      boolean b = l.tryLock(10000,java.util.concurrent.TimeUnit.MILLISECONDS);\r\n      if (b) {\r\n        \/\/LGWR should check this latch to see if it has work on which to wait...\r\n        \/\/What should we pass?  We could just pass the value, and hold the latch for that\r\n        \/\/buffer throughout the add process.  What did Oracle used to do?  You could\r\n        \/\/set log_small_buffer_size (or something like that), to encourage it to do more or\r\n        \/\/less copies.\r\n        System.out.println(\"Got the lock for \" + thrd.getName());\r\n        redoCopy((new Date()).toString());\r\n        \/\/Now, we need to get the redo allocation latch.  This should allocate\r\n        \/\/space in the vector.  Where should this vector live?\r\n        \/\/It should be a static part of this class of a fixed size (like log_buffer).\r\n        \/\/The allocation method should identify which block is free, and write to it.\r\n      }\r\n      else {\r\n        System.out.println(\"Didn't get the lock for \" + thrd.getName());\r\n      }\r\n      \/\/l.lock();\r\n      Thread.sleep(2000);\r\n      l.unlock();\r\n    }\r\n    catch(Exception e) {}\r\n    System.out.println(thrd.getName() + \" terminating.\");\r\n  }\r\n}\r\n\r\nclass myLock {\r\n  public static void main(String args[]) {\r\n    for (int j = 1; j <= Integer.parseInt(args[0]); j++) {\r\n      MyThread mt1 = new MyThread(j);\r\n    }\r\n  }\r\n}\r\n\r\nclass serveLocks {\r\n  static final int PORT = 6666;\r\n  static int i = 0;\r\n  public static void main(String[] args) throws IOException {\r\n    ServerSocket s = new ServerSocket(PORT);\r\n    System.out.println(\"Server Started\");\r\n    try {\r\n      while(true) {\r\n        Socket socket = s.accept();\r\n        try {\r\n          new MyThread(i++);\r\n        }\r\n        catch(Exception e) {\r\n          socket.close();\r\n        }\r\n      }\r\n    }\r\n    finally {\r\n      s.close();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OK, so while trying to understand Oracle&#8217;s redo latching mechanisms (we have been experiencing a nightmare with log file sync since switching to a new DMX subsystem), I found that java 1.5 has a new Lock class. How cool is&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2009\/04\/15\/java-lock-class-is-great-for-test-casing\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,25,22],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/45"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":15,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":1476,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/45\/revisions\/1476"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}