@Override public int read() throws IOException { while (true) { synchronized (buffer) { if (buffer.size() != 0) break; } if (closed) return -1; MThread.sleep(500); } synchronized (buffer) { return buffer.removeFirst(); } }
public MThread start() { synchronized (this) { if (thread != null) throw new IllegalThreadStateException(); Container container = new Container(); thread = new Thread(getGroup(),container); initThread(thread); thread.start(); } return this; }
@Override protected void initThread(Thread thread) { super.initThread(thread); thread.setDaemon(true); }
public void clear() { try { MThread.getWithTimeout(new ValueProvider<String>() { @Override public String getValue() throws Exception { synchronized (running) { if (running.isEmpty()) return ""; return null; } } }, 10000, false); } catch (TimeoutRuntimeException e) { log().i("Can't stop running jobs"); } jobs.clear(); queue.clear(); }
/** * Open and read a stream. It put the content of the file into the byte array. * @param is * @param buffer * @param offset * @param length * @throws IOException */ public static void readBinary(InputStream is, byte[] buffer, int offset, int length) throws IOException { if (is == null || buffer == null) return; do { int j = is.read(buffer, offset, length); if (j < 0) throw new EOFException(); if (j == 0) MThread.sleep(10); offset = offset + j; length = length - j; } while (length > 0); }
protected void doExecuteJobInternal(SchedulerJob job, boolean forced) { if (!job.setBusy(this)) { log().w("job is busy, reshedule",job.getName()); boolean isRunning = false; synchronized (running) { isRunning = running.contains(job); } try { if (!isRunning) { log().w("release job lock", job.getName()); job.releaseBusy(null); } else { log().w("found a running job in the scheduler queue", job.getName()); job.setNextExecutionTime(SchedulerJob.CALCULATE_NEXT); job.doSchedule(this); } } catch (Throwable t) { log().e("busy error",job.getName(),job,t); } return; } new MThread(new MyExecutor(job,forced)).start(); //TODO unsafe, monitor runtime use timeout or long runtime warnings, use maximal number of threads. be sure a job is running once }
public static void main(String args[]) { for (int i = 0; i < 100; i++) { long atomic = getCurrentTime(); long local = System.currentTimeMillis(); System.out.println("Atomic: " + atomic); System.out.println("Local : " + local + " " + (atomic-local)); System.out.flush(); MThread.sleep(100); } } }
public static FileLock aquireLock(File lockFile, long timeout) throws IOException, TimeoutException { if (!lockFile.exists()) lockFile.createNewFile(); @SuppressWarnings("resource") FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel(); FileLock lock = null; long start = System.currentTimeMillis(); while (true) { try { lock = channel.lock(); return lock; } catch (OverlappingFileLockException e) { MThread.sleep(200); } if (System.currentTimeMillis() - start > timeout) throw new TimeoutException(lockFile.getAbsolutePath()); } }
/** * Open and read a stream. It returns the content of the file as byte array. * @param is * @param close * @return file content * @throws IOException */ public static byte[] readBinary(InputStream is, boolean close) throws IOException { if (is == null) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; try { while (true) { int size = is.read(buffer); if (size < 0 ) break; if (size == 0) MThread.sleep(50); else baos.write(buffer, 0, size); } } catch (EOFException e) {} if (close) is.close(); return baos.toByteArray(); }
public static <T extends Object> T waitFor(Class<? extends T> ifc, long timeout) { long start = System.currentTimeMillis(); while (true) { try { T api = lookup(ifc); if (api != null) // should not happen return api; } catch (Throwable t) {} if (System.currentTimeMillis() - start > timeout) throw new TimeoutRuntimeException("timeout getting API",ifc); MThread.sleep(500); } }
/** * Open and read a stream. It put the content of the file into the byte array. * @param is * @param buffer * @param offset * @param length * @throws IOException */ public static void readBinary(ObjectInput is, byte[] buffer, int offset, int length) throws IOException { if (is == null || buffer == null) return; do { int j = is.read(buffer, offset, length); if (j < 0) throw new EOFException(); if (j == 0) MThread.sleep(10); offset = offset + j; length = length - j; } while (length > 0); }
/** * Open and read a file. It returns the content of the file as string. * @param _is * @return file content */ public static String readFile(Reader _is) { if (_is == null) return null; StringBuilder sb = new StringBuilder(); char[] buffer = new char[1024]; try { while (true) { int size = _is.read(buffer); if (size < 0) return sb.toString(); if (size > 0) { sb.append(buffer, 0, size); } else MThread.sleep(50); } } catch (EOFException eofe) { } catch (Exception e) { log.d(e); } return sb.toString(); }
@Override public void run() { while (true) { try { T val = provider.getValue(); if (nullAllowed || val != null) { value.value = val; return; } } catch (Throwable t) {} if (System.currentTimeMillis() - start > timeout) throw new TimeoutRuntimeException(); sleep(200); } } });
/** * Wait for the checker to return true or throw an TimeoutRuntimeException on timeout. * * @param checker * @param timeout * @throws Exception Thrown if checker throws an exception */ public static void waitForWithException( final Checker checker, long timeout) throws Exception { long start = System.currentTimeMillis(); while (true) { try { if (checker.check()) return; } catch (Throwable t) { throw t; } if (System.currentTimeMillis() - start > timeout) throw new TimeoutRuntimeException(); sleep(200); } }
/** * Wait for the checker to return true or throw an TimeoutRuntimeException on timeout. A exception * in the checker will be ignored. * * @param checker * @param timeout */ public static void waitFor( final Checker checker, long timeout) { long start = System.currentTimeMillis(); while (true) { try { if (checker.check()) return; } catch (Throwable t) { } if (System.currentTimeMillis() - start > timeout) throw new TimeoutRuntimeException(); sleep(200); } }
MThread.sleep(10); timeout--; if (timeout <=0)
@Override public void write(int b) throws IOException { synchronized(this) { long time = 0; while (buffer.size() >= maxBufferSize) { if (getWriteTimeout() >=0 && time >= getWriteTimeout()) throw new IOException("timeout", null); MThread.sleep(100); if (getWriteTimeout() >= 0) time+=100; } synchronized (buffer) { buffer.addLast((byte)b); } } }