jjzjj

Thread类

全部标签

java - Thread.holdsLock() 和锁粗化

假设我有2个相邻的synchronizedblock,它们之间有一个Thread.holdsLock()调用:finalObjectlock=newObject();//...synchronized(lock){//dostuff}if(Thread.holdsLock(lock)){thrownewIllegalStateException();}synchronized(lock){//domorestuff}现在,如果JVM在某个时候决定coarsen会怎样?锁定并合并上面的synchronizedblock?Thread.holdsLock()调用是否仍会返回false,或者

java - "Thread.currentThread().getName"和 "this.getName"有什么区别?

这是代码:importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.ThreadFactory;classUnCatchExceptionThreadextendsThread{publicUnCatchExceptionThread(Stringname){this.setName(name);}@Overridepublicvoidrun(){System.out.println("Threadnameis:"+this.get

java - 如何理解Java Thread中的wait和notify方法?

我对这两个描述感到很困惑:“等待方法阻塞调用线程并放弃监视器锁”“notify方法解除了一个等待线程的阻塞,但没有放弃监视器锁”这是我的问题:我知道Java中的每个对象都有一个锁,但是“监控锁”是什么意思呢?它和对象的锁一样吗?为什么notify方法需要放弃监听锁?如果我尝试使用以下代码让对象等待:classsimpleTaskextendsThread{intwaitingTime;publicsimpleTask(intwaitingTime){this.waitingTime=waitingTime;}publicvoidrun(){synchronized(this)//thi

java - 理解 Thread.currentThread().getContextClassLoader().getResourceAsStream()

我正在查看代码示例,但不确定这意味着什么。Thread.currentThread().getContextClassLoader().getResourceAsStream("MyProperty.properties");它似乎想要读取属性文件,但我不确定MyProperty.properties的位置。感谢您的帮助,谢谢。 最佳答案 ItappearsthatitlookingtoreadapropertyfilebutIamnotsurewhereMyProperty.propertiesislocated.正如您当前拥有的

java - 在 Callable 中处理 Thread.interrupted() 的正确方法?

在Callable中处理Thread.interrupted()的正确方法是什么?我猜可调用对象应该抛出一个InterruptedException;例如:publicclassMyCallableimplementsCallable{publicObjectcall(){Objectresult=null;//Simulatelong-runningoperationthatcalculatesresultwhile(true){...if(Thread.interrupted()){thrownewInterruptedException();}}result=...//somet

java - Java : what's the point? 中的 Thread.interrupt()

这个问题在这里已经有了答案:JavalongrunningtaskThreadinterruptvscancelflag(5个答案)关闭9年前。我完全理解它的作用(至少我希望如此)。它并没有真正中断线程。它使Thread.isInterrupted()为真,代码应该检查是什么方法并停止线程本身。我的问题是,为什么我们甚至需要这种方法?它似乎完全可以通过声明一个boolean标志来说明是否应该停止这个线程来替换?没有任何Java教科书使用这个boolean标志作为应如何使用volatile关键字的最佳示例吗?我特别困惑,因为似乎没有办法“不中断”线程,因为Thread.resume()已

java.lang.Thread - threadStatus 从何而来?

通过Javasourcecode我们有那个//variablenotwrittenorupdatedanywhereonthisclassprivatevolatileintthreadStatus=0;publicStategetState(){//getcurrentthreadstatereturnsun.misc.VM.toThreadState(threadStatus);}threadStatus如何以及在哪里更新?我们的想法是最终尝试使用AOP围绕更新方法进行编织,并在threadStatus更改时进行回调。 最佳答案

java - 替代 Thread.suspend() 和 .resume()

我有一大段代码不是循环,只是执行一次但需要一些时间的命令列表。我需要它根据不断变化的boolean值随时暂停或终止它。我可以使用不同的线程来挂起、恢复和停止此代码,但这些方法已被弃用,因此我想避免使用它们。我可以检查每一行代码之间的boolean值,但我希望有一个更优雅的解决方案。有什么好的方法吗? 最佳答案 Icouldcheckthebooleanbetweeneverylineofcode,butIamhopingforamoreelegantsolution.Isthereagoodwaytodothis?很遗憾,没有。要替

java - GWT、Google App Engine、TimerTask 或 ServiceImpl 中的 Thread 抛出异常

我正在使用GWT和GoogleAppEngine。我有一系列记录,我想每30分钟更新一次。在ServiceImpl中,我有以下代码:newTimer().schedule(newTimerTask(){@Overridepublicvoidrun(){try{Thread.sleep(30000);}catch(InterruptedExceptione){e.printStackTrace();}result=updateFeeds();}},30000,Long.MAX_VALUE);当我运行应用程序时,当我得到:com.google.gwt.user.server.rpc.Une

java - 从多个线程调用时 Thread.sleep() 如何工作

sleep()是Thread类的静态方法。从多个线程调用时它是如何工作的。以及它如何确定当前的执行线程。?或者可能是一个更通用的问题是如何从不同的线程调用静态方法?不会有任何并发​​问题吗? 最佳答案 howdoesitfigureoutthecurrentthreadofexecution?没必要。它只是调用操作系统,操作系统总是让调用它的线程hibernate。 关于java-从多个线程调用时Thread.sleep()如何工作,我们在StackOverflow上找到一个类似的问题: