Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2009, 2013 Andrew Gvozdev (Quoin Inc.). * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Andrew Gvozdev (Quoin Inc.) *******************************************************************************/ import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Main { /** * Workaround for Java problem on Windows with releasing buffers for memory-mapped files. * * @see "http://stackoverflow.com/questions/3602783/file-access-synchronized-on-java-object" * @see "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6354433" * @see "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4715154" * @see "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4469299" */ private static FileOutputStream getFileOutputStreamWorkaround(java.io.File storeFile) throws FileNotFoundException { final int maxCount = 10; for (int i = 0; i <= maxCount; i++) { try { // there is no sleep on first round Thread.sleep(10 * i); } catch (InterruptedException e) { // restore interrupted status Thread.currentThread().interrupt(); } try { return new FileOutputStream(storeFile); } catch (FileNotFoundException e) { // only apply workaround for the very specific exception if (i >= maxCount || !e.getMessage().contains( "The requested operation cannot be performed on a file with a user-mapped section open")) { //$NON-NLS-1$ throw e; } // CCorePlugin.log(new Status(IStatus.INFO, CCorePlugin.PLUGIN_ID, "Workaround for concurrent access to memory-mapped files applied, attempt " + (i + 1), e)); //$NON-NLS-1$ } } // will never get here return null; } }