org.openide.util.Mutex类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(161)

本文整理了Java中org.openide.util.Mutex类的一些代码示例,展示了Mutex类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mutex类的具体详情如下:
包路径:org.openide.util.Mutex
类名称: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:

  1. Mutex m = new Mutex();
  2. // Grant write access, compute an integer and return it:
  3. return m.writeAccess(new Mutex.Action<Integer>(){
  4. public Integer run() {
  5. return 1;
  6. }
  7. });
  8. // Obtain read access, do some computation,
  9. // possibly throw an IOException:
  10. try {
  11. m.readAccess(new Mutex.ExceptionAction<Void>() {
  12. public Void run() throws IOException {
  13. if (...) throw new IOException();
  14. return null;
  15. }
  16. });
  17. } catch (MutexException ex) {
  18. throw (IOException) ex.getException();
  19. }
  20. // check whether you are already in read access
  21. if (m.isReadAccess()) {
  22. // do your work
  23. }

[中]读多写一锁。允许控制可由多个读卡器同时读取但仅由一个编写器编写的资源。
如果你是一名作家,你也可以作为一名读者进入互斥锁。相反,如果你是“唯一”读卡器,你可以以写卡器的身份进入互斥锁,但你会收到警告,因为它很容易死锁(两个读卡器试图同时获得写访问权限)。
如果互斥锁仅由一个线程使用,则该线程可以作为编写器或读取器重复输入互斥锁。因此,一个线程永远不会自行死锁,无论以何种顺序执行操作。
没有防止饥饿的策略。即使有一位作者正在等待进入,另一位读者也可能会进入该部分。
使用示例:

  1. Mutex m = new Mutex();
  2. // Grant write access, compute an integer and return it:
  3. return m.writeAccess(new Mutex.Action<Integer>(){
  4. public Integer run() {
  5. return 1;
  6. }
  7. });
  8. // Obtain read access, do some computation,
  9. // possibly throw an IOException:
  10. try {
  11. m.readAccess(new Mutex.ExceptionAction<Void>() {
  12. public Void run() throws IOException {
  13. if (...) throw new IOException();
  14. return null;
  15. }
  16. });
  17. } catch (MutexException ex) {
  18. throw (IOException) ex.getException();
  19. }
  20. // check whether you are already in read access
  21. if (m.isReadAccess()) {
  22. // do your work
  23. }

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. Mutex.EVENT.readAccess(new Runnable() {
  2. public void run() {
  3. clearActionPerformers();

代码示例来源:origin: org.netbeans.api/org-openide-dialogs

  1. /** Setter for lists items.
  2. * @param content Array of list items.
  3. */
  4. public void setContent(final String[] content) {
  5. final JList list = contentList;
  6. if (list == null) {
  7. return;
  8. }
  9. // #18055: Ensure it runs in AWT thread.
  10. // Remove this when component handling will be assured
  11. // by other means that runs always in AWT.
  12. Mutex.EVENT.writeAccess(
  13. new Runnable() {
  14. @Override
  15. public void run() {
  16. list.setListData(content);
  17. list.revalidate();
  18. list.repaint();
  19. contentLabelPanel.setVisible(content.length > 0);
  20. }
  21. }
  22. );
  23. }

代码示例来源:origin: org.netbeans.api/org-openide-nodes

  1. public void reorder(final int[] perm) {
  2. MUTEX.postWriteRequest(new Runnable() {
  3. public void run() {
  4. Node[] n = nodes.toArray(new Node[nodes.size()]);
  5. List<Node> l = (List<Node>) nodes;
  6. for (int i = 0; i < n.length; i++) {
  7. l.set(perm[i], n[i]);
  8. }
  9. refresh();
  10. }
  11. });
  12. }

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. /** Tests whether this thread has already entered the mutex in write access.
  2. * If it returns true, calling <code>writeAccess</code> will be executed
  3. * immediatelly without any other blocking. <code>postReadAccess</code>
  4. * will be delayed until a write access runnable is over.
  5. *
  6. * @return true if the thread is in write access section
  7. * @since 4.48
  8. */
  9. public boolean isWriteAccess() {
  10. if (this == EVENT) {
  11. return javax.swing.SwingUtilities.isEventDispatchThread();
  12. }
  13. if (wrapper != null) {
  14. Mutex m = (Mutex)LOCK;
  15. return m.isWriteAccess();
  16. }
  17. Thread t = Thread.currentThread();
  18. ThreadInfo info;
  19. synchronized (LOCK) {
  20. info = getThreadInfo(t);
  21. if (info != null) {
  22. if (info.counts[X] > 0) {
  23. return true;
  24. }
  25. }
  26. }
  27. return false;
  28. }

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. return m.isReadAccess();
  2. info = getThreadInfo(t);

代码示例来源:origin: org.netbeans.api/org-openide-util

  1. if (m.isWriteAccess() || m.isReadAccess()) {
  2. run.run();
  3. } else {

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-makeproject

  1. /**
  2. * Try to load a config XML file from a named path.
  3. * If the file does not exist, return NONEXISTENT; or if there is any load error, return null.
  4. */
  5. private Document loadXml(String path) {
  6. assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
  7. assert Thread.holdsLock(modifiedMetadataPaths);
  8. FileObject xml = dir.getFileObject(path);
  9. if (xml == null || !xml.isData()) {
  10. return NONEXISTENT;
  11. }
  12. try {
  13. Document doc = XMLUtil.parse(new InputSource(xml.getInputStream()), false, true, XMLUtil.defaultErrorHandler(), null);
  14. return doc;
  15. } catch (IOException e) {
  16. if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
  17. LOG.log(Level.INFO, "Load XML: {0}", xml.getPath()); //NOI18N
  18. ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
  19. }
  20. } catch (SAXException e) {
  21. if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
  22. LOG.log(Level.INFO, "Load XML: {0}", xml.getPath()); //NOI18N
  23. ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
  24. }
  25. }
  26. return null;
  27. }

代码示例来源:origin: dcaoyuan/nbscala

  1. public void run() throws IOException {
  2. h[0] = createProject(dirFO, name, "src", "test", mainClass, manifestFile, manifestFile == null, librariesDefinition); //NOI18N
  3. final J2SEProject p = (J2SEProject) ProjectManager.getDefault().findProject(dirFO);
  4. ProjectManager.getDefault().saveProject(p);
  5. final ReferenceHelper refHelper = p.getReferenceHelper();
  6. try {
  7. ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
  8. public Void run() throws Exception {
  9. copyRequiredLibraries(h[0], refHelper);
  10. return null;
  11. }
  12. });
  13. } catch (MutexException ex) {
  14. Exceptions.printStackTrace(ex.getException());
  15. }
  16. FileObject srcFolder = dirFO.createFolder("src"); // NOI18N
  17. dirFO.createFolder("test"); // NOI18N
  18. if ( mainClass != null ) {
  19. createMainClass( mainClass, srcFolder );
  20. }
  21. }
  22. });

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-ruby-rakeproject

  1. /**
  2. * Try to load a config XML file from a named path.
  3. * If the file does not exist, or there is any load error, return null.
  4. */
  5. private Document loadXml(String path) {
  6. assert ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess();
  7. assert Thread.holdsLock(modifiedMetadataPaths);
  8. FileObject xml = dir.getFileObject(path);
  9. if (xml == null || !xml.isData()) {
  10. return null;
  11. }
  12. File f = FileUtil.toFile(xml);
  13. assert f != null;
  14. try {
  15. return XMLUtil.parse(new InputSource(f.toURI().toString()), false, true, XMLUtil.defaultErrorHandler(), null);
  16. } catch (IOException e) {
  17. if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
  18. ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
  19. }
  20. } catch (SAXException e) {
  21. if (!QUIETLY_SWALLOW_XML_LOAD_ERRORS) {
  22. ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
  23. }
  24. }
  25. return null;
  26. }

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-java-project-ui

  1. public void fileDataCreated( final FileEvent fe ) {
  2. FileObject fo = fe.getFile();
  3. if ( FileUtil.isParentOf( root, fo ) && isVisible( root, fo ) ) {
  4. if (ProjectManager.mutex().isReadAccess() || ProjectManager.mutex().isWriteAccess()) {
  5. PackageRootNode.PKG_VIEW_RP.post(new Runnable() {
  6. public void run() {
  7. fileDataCreated(fe);
  8. }
  9. });
  10. return;
  11. }
  12. FileObject parent = fo.getParent();
  13. if (!parent.isFolder()) {
  14. throw new IllegalStateException(FileUtil.getFileDisplayName(parent) + " is not a folder!"); //NOI18N
  15. }
  16. // XXX consider using group.contains() here
  17. if ( !VisibilityQuery.getDefault().isVisible( parent ) ) {
  18. return; // Adding file into ignored directory
  19. }
  20. PackageNode n = get( parent );
  21. if ( n == null && !contains( parent ) ) {
  22. add(parent, false, true);
  23. refreshKeysAsync();
  24. }
  25. else if ( n != null ) {
  26. n.updateChildren();
  27. }
  28. }
  29. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

  1. public void save() {
  2. try {
  3. // store properties
  4. ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
  5. @Override
  6. public Void run() throws IOException {
  7. saveProperties();
  8. saveCustomizerExtenders();
  9. ProjectManager.getDefault().saveProject(project);
  10. return null;
  11. }
  12. });
  13. } catch (MutexException e) {
  14. Exceptions.printStackTrace((IOException) e.getException());
  15. }
  16. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-profiler-projectsupport

  1. public static Properties getProjectProperties(final Project project) {
  2. final Properties props = new Properties();
  3. final FileObject propFile = project.getProjectDirectory().getFileObject("nbproject/project.properties"); // NOI18N
  4. if (propFile != null) {
  5. ProjectManager.mutex().readAccess(new Runnable() {
  6. public void run() {
  7. InputStream in = null;
  8. try {
  9. in = propFile.getInputStream();
  10. props.load(in);
  11. } catch (IOException ex) {
  12. LOGGER.finest("Could not load properties file: " + propFile.getPath()); // NOI18N
  13. } finally {
  14. if (in != null) {
  15. try {
  16. in.close();
  17. } catch (IOException ex) {
  18. // ignore
  19. }
  20. }
  21. }
  22. }
  23. });
  24. }
  25. return props;
  26. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

  1. @Override
  2. protected List<Node> getNodes() {
  3. List<Node> list = new ArrayList<>();
  4. // #172092
  5. List<FileObject> includePath = ProjectManager.mutex().readAccess(new Mutex.Action<List<FileObject>>() {
  6. @Override
  7. public List<FileObject> run() {
  8. return PhpSourcePath.getIncludePath(project.getProjectDirectory());
  9. }
  10. });
  11. for (FileObject fileObject : includePath) {
  12. if (fileObject != null && fileObject.isFolder()) {
  13. DataFolder df = DataFolder.findFolder(fileObject);
  14. list.add(new IncludePathNode(df, project));
  15. }
  16. }
  17. return list;
  18. }

代码示例来源:origin: dcaoyuan/nbscala

  1. public static DataObject create(final ScalaPlatform plat, final DataFolder f, final String idName) throws IOException {
  2. W w = new W(plat, f, idName);
  3. f.getPrimaryFile().getFileSystem().runAtomicAction(w);
  4. try {
  5. ProjectManager.mutex().writeAccess(
  6. new Mutex.ExceptionAction<Void> () {
  7. public Void run () throws Exception {
  8. EditableProperties props = PropertyUtils.getGlobalProperties();
  9. generatePlatformProperties(plat, idName, props);
  10. PropertyUtils.putGlobalProperties (props);
  11. return null;
  12. }
  13. });
  14. } catch (MutexException me) {
  15. Exception originalException = me.getException();
  16. if (originalException instanceof RuntimeException) {
  17. throw (RuntimeException) originalException;
  18. }
  19. else if (originalException instanceof IOException) {
  20. throw (IOException) originalException;
  21. }
  22. else
  23. {
  24. throw new IllegalStateException (); //Should never happen
  25. }
  26. }
  27. return w.holder;
  28. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-groovy-grailsproject

  1. public void save() {
  2. try {
  3. // store properties
  4. ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
  5. public Void run() throws IOException {
  6. saveProperties();
  7. return null;
  8. }
  9. });
  10. ProjectManager.getDefault().saveProject(project);
  11. } catch (MutexException e) {
  12. Exceptions.printStackTrace((IOException) e.getException());
  13. } catch (IOException ex) {
  14. Exceptions.printStackTrace(ex);
  15. }
  16. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-project

  1. public void run() throws IOException {
  2. ProjectManager.mutex().writeAccess(new Runnable() {
  3. public void run() {
  4. updateProject();
  5. }
  6. });
  7. }
  8. });

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javafx2-project

  1. public static EditableProperties readFromFile(final @NonNull FileObject propsFO) throws IOException {
  2. final EditableProperties ep = new EditableProperties(true);
  3. if(propsFO != null) {
  4. assert propsFO.isData();
  5. try {
  6. ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void>() {
  7. @Override
  8. public Void run() throws Exception {
  9. final InputStream is = propsFO.getInputStream();
  10. try {
  11. ep.load(is);
  12. } finally {
  13. if (is != null) {
  14. is.close();
  15. }
  16. }
  17. return null;
  18. }
  19. });
  20. } catch (MutexException mux) {
  21. throw (IOException) mux.getException();
  22. }
  23. }
  24. return ep;
  25. }

代码示例来源:origin: dcaoyuan/nbscala

  1. private void readPrivateProperties () {
  2. ProjectManager.mutex().readAccess(new Runnable() {
  3. public void run () {
  4. appArgs = project.getUpdateHelper().getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(J2SEProjectProperties.APPLICATION_ARGS);
  5. workDir = project.getUpdateHelper().getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(J2SEProjectProperties.RUN_WORK_DIR);
  6. }
  7. });
  8. }

代码示例来源:origin: org.netbeans.api/org-openide-explorer

  1. public GuardedActions(int type, Object p1) {
  2. this.type = type;
  3. this.p1 = p1;
  4. if (Children.MUTEX.isReadAccess() || Children.MUTEX.isWriteAccess()) {
  5. ret = run();
  6. } else {
  7. ret = Children.MUTEX.readAccess(this);
  8. }
  9. }

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-javafx2-project

  1. public static void saveToFile(final @NonNull FileObject propsFO, final @NonNull EditableProperties ep) throws IOException {
  2. if(propsFO != null) {
  3. assert propsFO.isData();
  4. try {
  5. ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void>() {
  6. @Override
  7. public Void run() throws Exception {
  8. OutputStream os = null;
  9. FileLock lock = null;
  10. try {
  11. lock = propsFO.lock();
  12. os = propsFO.getOutputStream(lock);
  13. ep.store(os);
  14. } finally {
  15. if (os != null) {
  16. os.close();
  17. }
  18. if (lock != null) {
  19. lock.releaseLock();
  20. }
  21. }
  22. return null;
  23. }
  24. });
  25. } catch (MutexException mux) {
  26. throw (IOException) mux.getException();
  27. }
  28. }
  29. }

相关文章