首页 \ 问答 \ Java线程生命周期(Java thread life time)

Java线程生命周期(Java thread life time)

假设我有以下内容:

public class A
{
    Thread t = new Thread(new Runnable() {
                      @Override
                            public void run() {

                    B b = new B();
                            }
                        });
    }

    class B{
     // long running process and alot of code
    }

问题是,B类所做的所有过程和工作都是在线程t下进行的,或者只是在创建了B类的对象并且B类的工作开始时才更有效?


Suppose I have the following:

public class A
{
    Thread t = new Thread(new Runnable() {
                      @Override
                            public void run() {

                    B b = new B();
                            }
                        });
    }

    class B{
     // long running process and alot of code
    }

The question is that all process and work that class B does goes under thread t or just when the object of class B is created and work of class B starts t is no more available?


原文:https://stackoverflow.com/questions/32167248
更新时间:2023-02-13 12:02

最满意答案

不,将CTRL-C字符传输到您的进程中将不起作用,因为CTRL-C键击被终端捕获并转换为发送到进程的终止信号。

CTRL-C的正确字符代码(ASCII码)是3号代码,但如果你将它回显给你的程序,它只会从它的标准输入接收字符。 它不会导致进程终止。

如果您想要简化查找和查杀流程的任务,您可以使用如下所示的内容:

./program_that_does_not_terminate &
pid=$!
sleep 20
kill ${pid}

$! 将为您提供上次启动的后台进程的进程ID。


No, piping a CTRL-C character into your process won't work, because a CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process.

The correct character code (in ASCII) for CTRL-C is code number 3 but, if you echo that to your program, it will simply receive the character from its standard input. It won't cause the process to terminate.

If you wanted to ease the task of finding and killing the process, you could use something like:

./program_that_does_not_terminate &
pid=$!
sleep 20
kill ${pid}

$! will give you the process ID for the last started background process.

相关问答

更多
  • 这实际上是特定于某些版本的echo (我很确定\c来自SysV,而-n版本是BSD-ism)。 它只是意味着不输出结尾的换行符。 That's actually specific to some versions of echo (I'm pretty sure that \c came from SysV while the -n version was a BSD-ism). It simply means don't output the trailing newline.
  • 不,将CTRL-C字符传输到您的进程中将不起作用,因为CTRL-C键击被终端捕获并转换为发送到进程的终止信号。 CTRL-C的正确字符代码(ASCII码)是3号代码,但如果你将它回显给你的程序,它只会从它的标准输入接收字符。 它不会导致进程终止。 如果您想要简化查找和查杀流程的任务,您可以使用如下所示的内容: ./program_that_does_not_terminate & pid=$! sleep 20 kill ${pid} $! 将为您提供上次启动的后台进程的进程ID。 No, piping ...
  • 有控制字符,你似乎喜欢一个空间。 例如看看ASCII 0-31 http://ascii-code.com/ 也许尝试复制粘贴到Windows编辑器(而不是Notepad ++或word)并粘贴回您的json数据。 there are control characters, which seems to you like a space. for example have a look at the ASCII 0-31 http://ascii-code.com/ Maybe try to copy-pa ...
  • 好的。 我找到了答案。 COMException(0x80004005):错误HRESULT E_FAIL发生,因为我试图从COM线程访问主线程对象。 以下是我的所作所为。 public partial class Form1 : Form { WebBrowser browser; SHDocVw.WebBrowser_V1 browserV1AceCounter; byte[] bPostData; string sHeader; string sURL; ...
  • for循环的目的是将\ n添加到字符串中。 循环之后你做 Password = [Password stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 这取代了添加的\ n,没有=删除它们。 BTW:使用15 \ n的字符串文字更容易,然后将填充长度的子字符串添加到源字符串: int padding = 16 - ([Password length] % 16); password = [password appendString:[@" ...
  • 您正在搜索的字符转义特定于Perl正则表达式。 为了您的目的,我建议您传递ctrl-M和ctrl-P的hex value 。 $exp->send("\x10"); # ctrl+P $exp->send("\x0D"); # ctrl+M 更新(测试): $exp->send("\x10"); # ctrl+P $exp->send("\n"); # send newline sleep 2; # wait for prompt $exp->expect($timeout, '$ '); $exp ...
  • 正如评论中所见 ,这解决了这个问题: grep $'\01' file As seen in comments, this solved the issue: grep $'\01' file
  • 你需要引用和逃避: set={1,2,3,"\'4\'"} 请注意,它与{1,2,3,4}一起使用,因为它是数字。 对于字符串,您还需要引用: {1,2,3,'a'} $ echo {1,2,3,'a'}{1,2,3,'a'} 11 12 13 1a 21 22 23 2a 31 32 33 3a a1 a2 a3 aa 然后转义是处理bash -c与值的执行。 根据您的评论,您的最终脚本是: set={"\(","\)","\,"} group=3 for ((i=0; i<$group; i++)) ...
  • 您不需要几百行批处理文件来执行替换就像这一样简单。 下面的两行批处理文件也是如此: @set @a=0 // & cscript //nologo //E:JScript "%~F0" < myfile.txt > out.txt & move /Y out.txt myfile.txt & goto :EOF WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/HEADER/g,"\x0C")); 有关正则表达式语法的进一步说明,请参阅此页面 ...
  • TUI密钥绑定有时不起作用,请参阅https://stackoverflow.com/a/30763087/72178和https://stackoverflow.com/a/30763033/72178 。 作为一种解决方法,您可以在最新版本的gdb中使用这些命令: tui enable tui disable TUI Key Bindings sometimes do not work, see https://stackoverflow.com/a/30763087/72178 and https: ...

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。