// Verify: do stress test, by opening IndexReaders and // IndexWriters over & over in 2 threads and making sure // no unexpected exceptions are raised: public void testStressLocks() throws Exception { Path tempPath = createTempDir(); assumeFalse("cannot handle buggy Files.delete", TestUtil.hasWindowsFS(tempPath)); Directory dir = getDirectory(tempPath); // First create a 1 doc index: IndexWriter w = new IndexWriter( dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE)); addDoc(w); w.close(); WriterThread writer = new WriterThread(100, dir); SearcherThread searcher = new SearcherThread(100, dir); writer.start(); searcher.start(); while (writer.isAlive() || searcher.isAlive()) { Thread.sleep(1000); } assertTrue("IndexWriter hit unexpected exceptions", !writer.hitException); assertTrue("IndexSearcher hit unexpected exceptions", !searcher.hitException); dir.close(); }
/** * Releases any resources held by the writer. * * @param shutdownWrapped If the wrapped writer should be closed as well. */ public void shutdown(boolean shutdownWrapped) { stopRequested.set(true); // Wait for publisher thread to terminate while (writerThread != null && writerThread.isAlive()) { try { // Interrupt the thread if its blocking writerThread.interrupt(); writerThread.join(); } catch (InterruptedException ex) { // Ignore; we gotta wait.. } } // The writer writerThread SHOULD have drained the queue. // If not, handle outstanding requests ourselves, // and push them to the writer. while (!queue.isEmpty()) { String message = queue.poll(); writer.writeRecord(message); } // Shutdown the wrapped writer. if (shutdownWrapped && writer != null) writer.shutdown(); DirectoryServer.deregisterShutdownListener(this); }
private void releaseRes() { if (nLogThread != null) { nLogThread.shutdown(); } if (nWaitLogs != null) { nWaitLogs.clear(); } }
private Logger() { CharSequence time = DateFormat.format("yyyy-MM-dd", System.currentTimeMillis()); File logDir = new File(FILE_PATH); if (!logDir.exists()) { logDir.mkdirs(); } Log.e(TAG, "" + logDir.exists()); nLogFile = new File(logDir, time + ".txt"); nWaitLogs = new ArrayBlockingQueue<String>(QUEUE_SIZE, true); nLogThread = new WriterThread(nWaitLogs); nLogThread.start(); }
/** * Construct a new ParallelTextWriter wrapper. * * @param name the name of the thread. * @param autoFlush indicates if the underlying writer should be flushed after the queue is * flushed. * @param writer a character stream used for output. */ public ParallelTextWriter(String name, boolean autoFlush, TextWriter writer) { this.name = name; this.autoFlush = autoFlush; this.writer = writer; this.queue = new ConcurrentLinkedQueue<String>(); this.writerThread = null; this.stopRequested = new AtomicBoolean(false); writerThread = new WriterThread(); writerThread.start(); DirectoryServer.registerShutdownListener(this); }