首页 \ 问答 \ 线程中断标志语义(thread interrupted flag semantics)

线程中断标志语义(thread interrupted flag semantics)

我在理解Java的线程中断标志的语义方面遇到了一些麻烦。 我的理解是该标志只应在线程被中断后才为真,并且一旦设置为true将不再为假,直到已捕获InterruptedException或等效或使用.interrupted()显式清除该标志。 因此,我无法解释为什么以下程序打印false

Thread t = new Thread() {
    @Override
    public void run() {
        try {
            // While await()ing, another thread calls t.interrupt().
            new CyclicBarrier(2).await();
        } finally {
            // I believe I should still be interrupted here, but am not...
            System.out.println(Thread.currentThread().isInterrupted());
        }
    }
};

(为简单起见,排除了一些细节 - 假设run()可以抛出异常。)


I'm having a bit of trouble understanding the semantics for Java's thread interrupted flag. My understanding is that the flag should only be true after a thread is interrupted, and once set to true will not be false again until the InterruptedException or equivalent has been caught or the flag is explicitly cleared with .interrupted(). Hence, I am not able to explain why the following program prints false:

Thread t = new Thread() {
    @Override
    public void run() {
        try {
            // While await()ing, another thread calls t.interrupt().
            new CyclicBarrier(2).await();
        } finally {
            // I believe I should still be interrupted here, but am not...
            System.out.println(Thread.currentThread().isInterrupted());
        }
    }
};

(Some details excluded for simplicity - assume it's ok for run() to throw exceptions.)


原文:https://stackoverflow.com/questions/21213783
更新时间:2023-09-20 14:09

最满意答案

作为一个实验,我创建了一个新的ASP.NET Webforms项目,并将其部署到IIS。 然后,我创建了第二个.NET类库,并将该.dll文件复制到Web应用程序的\bin文件夹中(该类库未在ASP.NET应用程序中的任何位置引用或使用)。

我启动了SysInternals ProcMon,在IIS中回收了应用程序池和网站,并在浏览器中请求该网站。

w3wp.exe确实在首页请求中读取了类库.dll文件。


此MSDN页面还指出:

您可以将编译的程序集存储在Bin文件夹中,并且Web应用程序中任何位置的其他代码(例如页面代码)都会自动引用它。 一个典型的例子是你有一个自定义类的编译代码。 您可以将编译后的程序集复制到Web应用程序的Bin文件夹中,然后该类即可用于所有页面。

Bin文件夹中的程序集不需要安装在全局程序集缓存(GAC)中。 Bin文件夹中存在.dll文件足以让ASP.NET识别它。

这似乎暗示ASP.NET将反映它在\bin找到的程序集并自动加载它们。


有趣的是,即使你把一个非.NET文件(我将C:\ Windows中的twain.dll拷贝到你的ASP.NET bin文件夹中),也可以读取这些文件。 运行时似乎只是向文件系统询问\bin\*并在文件上循环以检查要加载的.NET程序集。


我也注意到,如果你添加到你的web.config文件中:

  <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <clear />
      </assemblies>
    </compilation>

然后页面将不再运行,并显示错误

无法加载类型'WebApplication1.Global'。

所以似乎运行时不再从程序集中加载这些类。 但是 ,运行时仍然会从驱动器中读取未引用的控制台应用程序.dll和non.net程序集twain.dll。

因此,答案归结为“加载所有dll”的含义......如果您的意思是在运行时提供,那么如果您指定了自己的system.web | compilation | assemblies ,那么答案就是“否” system.web | compilation | assemblies system.web | compilation | assemblies system.web | compilation | assemblies但默认是加载全部。 但是,如果你指的是什么文件被物理读取,那么“是”。


As an experiment I made a new ASP.NET Webforms project, and deployed it to IIS. I then made a 2nd .net class library, and copied the .dll file to the web app's \bin folder (the class library is not referenced or used anywhere in the ASP.NET app).

I started up SysInternals ProcMon, recycled the app pool and web site in IIS, and requested the site in a browser.

w3wp.exe does indeed read the class library .dll file on first page request.


This MSDN page also states:

You can store compiled assemblies in the Bin folder, and other code anywhere in the web application (such as code for pages) automatically references it. A typical example is that you have the compiled code for a custom class. You can copy the compiled assembly to the Bin folder of your Web application and the class is then available to all pages.

Assemblies in the Bin folder do not need to be installed in the Global Assembly Cache (GAC). The presence of a .dll file in the Bin folder is sufficient for ASP.NET to recognize it.

Which does seem to imply that ASP.NET will reflect over the assemblies it finds in \bin and automatically load them.


Interestingly, even if you put a non-.net file (I copied twain.dll from C:\Windows) into your ASP.NET bin folder, those files are also read. The runtime seems to just ask the filesystem for \bin\* and loops over the files to check for .NET assemblies to load.


I also noticed that if you add this to your web.config file:

  <system.web>
    <compilation targetFramework="4.5">
      <assemblies>
        <clear />
      </assemblies>
    </compilation>

Then the page will no longer run, with the error

Could not load type 'WebApplication1.Global'.

So it seems that the runtime no longer loads those classes from the assemblies. However, the runtime still reads the non-referenced console application .dll and non-.net assembly twain.dll off the drive.

So, the answer comes down to what you mean by "loads all dlls" ... If you mean makes available in the runtime, then the answer is sort-of "no" if you specify your own system.web | compilation | assemblies but the default is to load all. But if you mean what files are physically read, then "yes".

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。