本文整理了Java中org.openide.util.Mutex
类的一些代码示例,展示了Mutex
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mutex
类的具体详情如下:
包路径:org.openide.util.Mutex
类名称:Mutex
[英]Read-many/write-one lock. Allows control over resources that can be read by several readers at once but only written by one writer.
It is guaranteed that if you are a writer you can also enter the mutex as a reader. Conversely, if you are the only reader you can enter the mutex as a writer, but you'll be warned because it is very deadlock prone (two readers trying to get write access concurently).
If the mutex is used only by one thread, the thread can repeatedly enter it as a writer or reader. So one thread can never deadlock itself, whichever order operations are performed in.
There is no strategy to prevent starvation. Even if there is a writer waiting to enter, another reader might enter the section instead.
Examples of use:
Mutex m = new Mutex();
// Grant write access, compute an integer and return it:
return m.writeAccess(new Mutex.Action<Integer>(){
public Integer run() {
return 1;
}
});
// Obtain read access, do some computation,
// possibly throw an IOException:
try {
m.readAccess(new Mutex.ExceptionAction<Void>() {
public Void run() throws IOException {
if (...) throw new IOException();
return null;
}
});
} catch (MutexException ex) {
throw (IOException) ex.getException();
}
// check whether you are already in read access
if (m.isReadAccess()) {
// do your work
}
[中]读多写一锁。允许控制可由多个读卡器同时读取但仅由一个编写器编写的资源。
如果你是一名作家,你也可以作为一名读者进入互斥锁。相反,如果你是“唯一”读卡器,你可以以写卡器的身份进入互斥锁,但你会收到警告,因为它很容易死锁(两个读卡器试图同时获得写访问权限)。
如果互斥锁仅由一个线程使用,则该线程可以作为编写器或读取器重复输入互斥锁。因此,一个线程永远不会自行死锁,无论以何种顺序执行操作。
没有防止饥饿的策略。即使有一位作者正在等待进入,另一位读者也可能会进入该部分。
使用示例:
Mutex m = new Mutex();
// Grant write access, compute an integer and return it:
return m.writeAccess(new Mutex.Action<Integer>(){
public Integer run() {
return 1;
}
});
// Obtain read access, do some computation,
// possibly throw an IOException:
try {
m.readAccess(new Mutex.ExceptionAction<Void>() {
public Void run() throws IOException {
if (...) throw new IOException();
return null;
}
});
} catch (MutexException ex) {
throw (IOException) ex.getException();
}
// check whether you are already in read access
if (m.isReadAccess()) {
// do your work
}
代码示例来源:origin: org.netbeans.api/org-openide-util
Mutex.EVENT.readAccess(new Runnable() {
public void run() {
clearActionPerformers();
代码示例来源:origin: org.netbeans.api/org-openide-dialogs
/** Setter for lists items.
* @param content Array of list items.
*/
public void setContent(final String[] content) {
final JList list = contentList;
if (list == null) {
return;
}
// #18055: Ensure it runs in AWT thread.
// Remove this when component handling will be assured
// by other means that runs always in AWT.
Mutex.EVENT.writeAccess(
new Runnable() {
@Override
public void run() {
list.setListData(content);
list.revalidate();
list.repaint();
contentLabelPanel.setVisible(content.length > 0);
}
}
);
}
代码示例来源:origin: org.netbeans.api/org-openide-nodes
public void reorder(final int[] perm) {
MUTEX.postWriteRequest(new Runnable() {
public void run() {
Node[] n = nodes.toArray(new Node[nodes.size()]);
List<Node> l = (List<Node>) nodes;
for (int i = 0; i < n.length; i++) {
l.set(perm[i], n[i]);
}
refresh();
}
});
}
代码示例来源:origin: org.netbeans.api/org-openide-util
/** Tests whether this thread has already entered the mutex in write access.
* If it returns true, calling <code>writeAccess</code> will be executed
* immediatelly without any other blocking. <code>postReadAccess</code>
* will be delayed until a write access runnable is over.
*
* @return true if the thread is in write access section
* @since 4.48
*/
public boolean isWriteAccess() {
if (this == EVENT) {
return javax.swing.SwingUtilities.isEventDispatchThread();
}
if (wrapper != null) {
Mutex m = (Mutex)LOCK;
return m.isWriteAccess();
}
Thread t = Thread.currentThread();
ThreadInfo info;
synchronized (LOCK) {
info = getThreadInfo(t);
if (info != null) {
if (info.counts[X] > 0) {
return true;
}
}
}
return false;
}
代码示例来源:origin: org.netbeans.api/org-openide-util
return m.isReadAccess();
info = getThreadInfo(t);
代码示例来源:origin: org.netbeans.api/org-openide-util
if (m.isWriteAccess() || m.isReadAccess()) {
run.run();
} else {
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-makeproject
/**
* Try to load a config XML file from a named path.
* If the file does not exist, return NONEXISTENT; or if there is any load error, return null.
*/
private Document loadXml(String path) {
assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
assert Thread.holdsLock(modifiedMetadataPaths);
FileObject xml = dir.getFileObject(path);
if (xml == null || !xml.isData()) {
return NONEXISTENT;
}
try {
Document doc = XMLUtil.parse(new InputSource(xml.getInputStream()), false, true, XMLUtil.defaultErrorHandler(), null);
return doc;
} catch (IOException e) {
if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
LOG.log(Level.INFO, "Load XML: {0}", xml.getPath()); //NOI18N
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
} catch (SAXException e) {
if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
LOG.log(Level.INFO, "Load XML: {0}", xml.getPath()); //NOI18N
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
}
return null;
}
代码示例来源:origin: dcaoyuan/nbscala
public void run() throws IOException {
h[0] = createProject(dirFO, name, "src", "test", mainClass, manifestFile, manifestFile == null, librariesDefinition); //NOI18N
final J2SEProject p = (J2SEProject) ProjectManager.getDefault().findProject(dirFO);
ProjectManager.getDefault().saveProject(p);
final ReferenceHelper refHelper = p.getReferenceHelper();
try {
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
public Void run() throws Exception {
copyRequiredLibraries(h[0], refHelper);
return null;
}
});
} catch (MutexException ex) {
Exceptions.printStackTrace(ex.getException());
}
FileObject srcFolder = dirFO.createFolder("src"); // NOI18N
dirFO.createFolder("test"); // NOI18N
if ( mainClass != null ) {
createMainClass( mainClass, srcFolder );
}
}
});
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-ruby-rakeproject
/**
* Try to load a config XML file from a named path.
* If the file does not exist, or there is any load error, return null.
*/
private Document loadXml(String path) {
assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
assert Thread.holdsLock(modifiedMetadataPaths);
FileObject xml = dir.getFileObject(path);
if (xml == null || !xml.isData()) {
return null;
}
File f = FileUtil.toFile(xml);
assert f != null;
try {
return XMLUtil.parse(new InputSource(f.toURI().toString()), false, true, XMLUtil.defaultErrorHandler(), null);
} catch (IOException e) {
if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
} catch (SAXException e) {
if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
}
return null;
}
代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-project-ui
public void fileDataCreated( final FileEvent fe ) {
FileObject fo = fe.getFile();
if ( FileUtil.isParentOf( root, fo ) && isVisible( root, fo ) ) {
if (ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess()) {
PackageRootNode.PKG_VIEW_RP.post(new Runnable() {
public void run() {
fileDataCreated(fe);
}
});
return;
}
FileObject parent = fo.getParent();
if (!parent.isFolder()) {
throw new IllegalStateException(FileUtil.getFileDisplayName(parent) + " is not a folder!"); //NOI18N
}
// XXX consider using group.contains() here
if ( !VisibilityQuery.getDefault().isVisible( parent ) ) {
return; // Adding file into ignored directory
}
PackageNode n = get( parent );
if ( n == null && !contains( parent ) ) {
add(parent, false, true);
refreshKeysAsync();
}
else if ( n != null ) {
n.updateChildren();
}
}
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project
public void save() {
try {
// store properties
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
@Override
public Void run() throws IOException {
saveProperties();
saveCustomizerExtenders();
ProjectManager.getDefault().saveProject(project);
return null;
}
});
} catch (MutexException e) {
Exceptions.printStackTrace((IOException) e.getException());
}
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-profiler-projectsupport
public static Properties getProjectProperties(final Project project) {
final Properties props = new Properties();
final FileObject propFile = project.getProjectDirectory().getFileObject("nbproject/project.properties"); // NOI18N
if (propFile != null) {
ProjectManager.mutex().readAccess(new Runnable() {
public void run() {
InputStream in = null;
try {
in = propFile.getInputStream();
props.load(in);
} catch (IOException ex) {
LOGGER.finest("Could not load properties file: " + propFile.getPath()); // NOI18N
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
// ignore
}
}
}
}
});
}
return props;
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project
@Override
protected List<Node> getNodes() {
List<Node> list = new ArrayList<>();
// #172092
List<FileObject> includePath = ProjectManager.mutex().readAccess(new Mutex.Action<List<FileObject>>() {
@Override
public List<FileObject> run() {
return PhpSourcePath.getIncludePath(project.getProjectDirectory());
}
});
for (FileObject fileObject : includePath) {
if (fileObject != null && fileObject.isFolder()) {
DataFolder df = DataFolder.findFolder(fileObject);
list.add(new IncludePathNode(df, project));
}
}
return list;
}
代码示例来源:origin: dcaoyuan/nbscala
public static DataObject create(final ScalaPlatform plat, final DataFolder f, final String idName) throws IOException {
W w = new W(plat, f, idName);
f.getPrimaryFile().getFileSystem().runAtomicAction(w);
try {
ProjectManager.mutex().writeAccess(
new Mutex.ExceptionAction<Void> () {
public Void run () throws Exception {
EditableProperties props = PropertyUtils.getGlobalProperties();
generatePlatformProperties(plat, idName, props);
PropertyUtils.putGlobalProperties (props);
return null;
}
});
} catch (MutexException me) {
Exception originalException = me.getException();
if (originalException instanceof RuntimeException) {
throw (RuntimeException) originalException;
}
else if (originalException instanceof IOException) {
throw (IOException) originalException;
}
else
{
throw new IllegalStateException (); //Should never happen
}
}
return w.holder;
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-groovy-grailsproject
public void save() {
try {
// store properties
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
public Void run() throws IOException {
saveProperties();
return null;
}
});
ProjectManager.getDefault().saveProject(project);
} catch (MutexException e) {
Exceptions.printStackTrace((IOException) e.getException());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-project
public void run() throws IOException {
ProjectManager.mutex().writeAccess(new Runnable() {
public void run() {
updateProject();
}
});
}
});
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javafx2-project
public static EditableProperties readFromFile(final @NonNull FileObject propsFO) throws IOException {
final EditableProperties ep = new EditableProperties(true);
if(propsFO != null) {
assert propsFO.isData();
try {
ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void>() {
@Override
public Void run() throws Exception {
final InputStream is = propsFO.getInputStream();
try {
ep.load(is);
} finally {
if (is != null) {
is.close();
}
}
return null;
}
});
} catch (MutexException mux) {
throw (IOException) mux.getException();
}
}
return ep;
}
代码示例来源:origin: dcaoyuan/nbscala
private void readPrivateProperties () {
ProjectManager.mutex().readAccess(new Runnable() {
public void run () {
appArgs = project.getUpdateHelper().getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(J2SEProjectProperties.APPLICATION_ARGS);
workDir = project.getUpdateHelper().getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(J2SEProjectProperties.RUN_WORK_DIR);
}
});
}
代码示例来源:origin: org.netbeans.api/org-openide-explorer
public GuardedActions(int type, Object p1) {
this.type = type;
this.p1 = p1;
if (Children.MUTEX.isReadAccess() || Children.MUTEX.isWriteAccess()) {
ret = run();
} else {
ret = Children.MUTEX.readAccess(this);
}
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javafx2-project
public static void saveToFile(final @NonNull FileObject propsFO, final @NonNull EditableProperties ep) throws IOException {
if(propsFO != null) {
assert propsFO.isData();
try {
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
@Override
public Void run() throws Exception {
OutputStream os = null;
FileLock lock = null;
try {
lock = propsFO.lock();
os = propsFO.getOutputStream(lock);
ep.store(os);
} finally {
if (os != null) {
os.close();
}
if (lock != null) {
lock.releaseLock();
}
}
return null;
}
});
} catch (MutexException mux) {
throw (IOException) mux.getException();
}
}
}
内容来源于网络,如有侵权,请联系作者删除!