1010/**
1111 * @author Rob Austin.
1212 */
13- class LockCheck {
13+ enum LockCheck {
14+ ;
1415
1516 static final String TMP = System .getProperty ("java.io.tmpdir" );
1617 public static final String TARGET = System .getProperty ("project.build.directory" , findTarget ());
1718 private static final String OS = System .getProperty ("os.name" ).toLowerCase ();
1819 static final boolean IS_LINUX = OS .startsWith ("linux" );
19- private ThreadLocal <SimpleDateFormat > df = new ThreadLocal () {
20-
21- @ Override
22- protected Object initialValue () {
23- return new SimpleDateFormat ("yyyy.MM" + ".dd 'at' HH:mm:ss z" );
24- }
25- };
20+ private static SimpleDateFormat df = new SimpleDateFormat ("yyyy.MM" + ".dd 'at' HH:mm:ss z" );
2621
2722 private static String findTarget () {
2823 for (File dir = new File (System .getProperty ("user.dir" )); dir != null ; dir = dir .getParentFile ()) {
@@ -33,74 +28,44 @@ private static String findTarget() {
3328 return TMP + "/target" ;
3429 }
3530
36- public static long getPID () {
31+ static long getPID () {
3732 String processName =
3833 java .lang .management .ManagementFactory .getRuntimeMXBean ().getName ();
3934 return Long .parseLong (processName .split ("@" )[0 ]);
4035 }
4136
42- boolean isFreeCpu (int cpu ) throws IOException {
43- long processID = getPID ();
37+ static boolean isCpuFree (int cpu ) throws IOException {
4438
4539 final File file = toFile (cpu );
4640 final boolean exists = file .exists ();
4741
4842 if (!exists ) {
49- // create a lock file
50- storePid (processID , file );
51- return true ;
52- } else {
53- int currentProcess = getProcessForCore (file );
54- if (!isProcessRunning (currentProcess )) {
55- replacePid (file , processID );
56- return true ;
57- }
58- return false ;
59- }
60- }
61-
62- /**
63- * stores the process id for a give pid
64- *
65- * @param core
66- * @param processID
67- * @return
68- * @throws IOException
69- */
70- private boolean isFreeCpu (int core , long processID ) throws IOException {
71-
72- final File file = toFile (core );
73- final boolean exists = file .exists ();
74-
75- if (!exists ) {
76- // create a lock file
77- storePid (processID , file );
7843 return true ;
7944 } else {
80- int currentProcess = getProcessForCore (file );
45+ int currentProcess = getProcessForCpu (file );
8146 if (!isProcessRunning (currentProcess )) {
82- replacePid ( file , processID );
47+ file . delete ( );
8348 return true ;
8449 }
8550 return false ;
8651 }
8752 }
8853
8954 @ NotNull
90- private File toFile (int core ) {
55+ private static File toFile (int core ) {
9156 return new File (tmpDir (), "cpu-" + core + ".lock" );
9257 }
9358
94- void replacePid (int core , long processID ) throws IOException {
59+ static void replacePid (int core , long processID ) throws IOException {
9560 replacePid (toFile (core ), processID );
9661 }
9762
98- private void replacePid (File file , long processID ) throws IOException {
63+ private static void replacePid (File file , long processID ) throws IOException {
9964 file .delete ();
10065 storePid (processID , file );
10166 }
10267
103- boolean isProcessRunning (long pid ) {
68+ static boolean isProcessRunning (long pid ) {
10469 if (IS_LINUX )
10570 return new File ("/proc/" + pid ).exists ();
10671 else
@@ -115,28 +80,27 @@ boolean isProcessRunning(long pid) {
11580 * @param coreFile
11681 * @throws IOException
11782 */
118- private void storePid (long processID , File coreFile ) throws IOException {
119- try ( Writer writer = new BufferedWriter ( new OutputStreamWriter (
120- new FileOutputStream ( coreFile ), "utf-8" ))) {
121- String processIDStr = Long .toString (processID );
122- writer .write (processIDStr + "\n " + df .get (). format (new Date ()));
123- }
83+ private synchronized static void storePid (long processID , File coreFile ) throws IOException {
84+ RandomAccessFile f = new RandomAccessFile ( coreFile , "rw" );
85+ f . seek ( 0 ); // to the beginning
86+ String processIDStr = Long .toString (processID );
87+ f .write (( processIDStr + "\n " + df .format (new Date ())). getBytes ( ));
88+ f . close ();
12489 }
12590
126- int getProcessForCore (int core ) throws IOException {
127- return getProcessForCore (toFile (core ));
91+ static int getProcessForCpu (int core ) throws IOException {
92+ return getProcessForCpu (toFile (core ));
12893 }
12994
130- private int getProcessForCore (@ NotNull File coreFile ) throws IOException {
131-
95+ private static int getProcessForCpu (@ NotNull File coreFile ) throws IOException {
13296 try (LineNumberReader reader = new LineNumberReader (
13397 new BufferedReader (new InputStreamReader (new FileInputStream (coreFile ), "utf-8" )))) {
13498 String s = reader .readLine ().trim ();
13599 return Integer .parseInt (s );
136100 }
137101 }
138102
139- private File tmpDir () {
103+ private static File tmpDir () {
140104 final File tempDir = new File (System .getProperty ("java.io.tmpdir" ));
141105
142106 if (!tempDir .exists ())
@@ -145,4 +109,11 @@ private File tmpDir() {
145109 return tempDir ;
146110 }
147111
112+ static void updateCpu (int cpu ) {
113+ try {
114+ replacePid (toFile (cpu ), getPID ());
115+ } catch (IOException e ) {
116+ e .printStackTrace ();
117+ }
118+ }
148119}
0 commit comments