草庐IT

java - 如何避免使用 Thread.Sleep

在我下载最新版本的Netbeans之前,我从未考虑过Thread.Sleep的使用。Netbeans现在警告您不要使用Thread.Sleep。因此,我对该主题进行了一些研究,发现有人说您只需要将Thread.Sleep用于调试/测试目的,如果您在任何其他时间使用它,您的代码就会写得不好。所以我的问题是在以下情况下如何避免使用Thread.Sleep。我编写了一个与另一个应用程序交互的服务器应用程序。服务器有两个线程:处理来自套接字的数据并发回其他信息或只是简单的确认。这是主线程。启动套接字线程后,它会进入一个无限期的while循环。在这个while循环中,我检查以确保套接字线程仍然处

java - 故障安全迭代器的逻辑是什么?

如果故障安全迭代器创建一个单独的副本并对其进行处理,它怎么会知道对原始副本所做的任何更改?publicclassconcurrentHashMap{publicstaticvoidmain(String[]args)throwsInterruptedException{MapCheckobj1=newMapCheck();Threadt1=newThread(newRunnable(){@Overridepublicvoidrun(){obj1.put();}});Threadt2=newThread(newRunnable(){@Overridepublicvoidrun(){obj

java - Java 中线程中的线程?

我目前正在考虑如何用Java设计一个多线程系统,需要做一些繁重的网络处理和数据库存储。该程序首先会启动三个基本线程。沿着这些基本线程,我想不是从主程序而是从两个线程启动其他线程。一个线程是否有可能启动另一个线程导致某种层次结构,例如:>Parent->t0thread1->t1tread1.1>->t0thread2>->t0thread3->t2thread3.1t0=initaltimet1,t2=timeatapointintherunningthreadt1!=t2如果不能,有人可以提供带有引用的理论解决方案吗? 最佳答案

java.rmi.ServerException : RemoteException occurred in server thread (ClassNotFoundException) 异常

以下方法:privatevoidstartServer(){//snippetthatstartstheserveronthelocalmachinetry{RemoteMethodImplimpl=newRemoteMethodImpl();Naming.rebind("Illusive-Server",impl);}catch(Exceptionexc){JOptionPane.showMessageDialog(this,"Problemstartingtheserver","Error",JOptionPane.ERROR_MESSAGE);System.out.println

java - 在 IntelliJ 插件中创建后台任务

我正在开发一个IntelliJ-idea插件并希望在后台任务中运行代码(在后台任务对话框和UI之外的另一个线程中可见)。我找到了以下Helperclass并通过传递一个Runnable对象并实现它的run方法来尝试它,但它仍然阻塞UI,当我尝试自己执行线程时,我收到以下错误Readaccessisallowedfromeventdispatchthreadorinsideread-actiononly(seecom.intellij.openapi.application.Application.runReadAction())Details:Currentthread:Thread[

java - Thread.sleep 等待时间超过预期

以下代码:longmsBefore=System.currentTimeMillis();//Thread.currentThread().setPriority(Thread.MAX_PRIORITY);try{Thread.sleep(200);}catch(InterruptedExceptione){}System.out.println("Time:"+(System.currentTimeMillis()-msBefore));打印:Time:578Time:594Time:625Time:640Time:641Time:609Time:625Time:625Time:61

java - 通过 Id 获取线程

我在JMXjava中工作,我通过使用ThreadMXBean接口(interface)的getAllThreadIds()方法获取所有线程ID,但我需要一种方法来终止线程给定的ID。例如:ThreadMXBeantbean;tbean=ManagementFactory.getThreadMXBean();long[]IDs=tbean.getAllThreadIds();//....IneedawaytokilltheThreadswhichhavethisIDs 最佳答案 你可以试试这个:publicvoidprintAllTh

java - hibernate 异常 : Could not obtain transaction-synchronized Session for current thread

我遇到错误:Exceptioninthread"main"org.hibernate.HibernateException:Couldnotobtaintransaction-synchronizedSessionforcurrentthread主要ppService.deleteProductPart(cPartId,productId);@Service("productPartService")@OverridepublicvoiddeleteProductPart(intcPartId,intproductId){productPartDao.deleteProductPart

java - Thread-Safe在java中是什么意思或者什么时候我们叫Thread-Safe?

我不理解这个概念。publicclassSomeName{publicstaticvoidmain(Stringargs[]){}}这是我的类(class)SomeName。现在这里的线程是什么。我们是否将类称为线程。当其他对象试图访问它的方法或成员时,我们是否将此类称为线程?当其他对象试图访问这个对象时,我们是否将这个类称为线程?当我们在java中将某些东西称为线程安全时,这意味着什么? 最佳答案 线程安全意味着避免一些问题。最常见也可能是最糟糕的称为线程锁。古老的类比是哲学家用餐的故事。他们非常有礼貌,不会在别人夹菜的时候伸出筷

java - JOptionPane.showMessageDialog 线程安全吗?

JOptionPane.showMessageDialog应该是获取用户反馈的有用实用程序,因为它会在您等待时阻塞当前线程。因此我希望它是线程安全的,并且您不需要将调用包装在invokeLater或invokeAndWait中。是这样吗? 最佳答案 取自javax.swing包说明:Swing'sThreadingPolicyIngeneralSwingisnotthreadsafe.AllSwingcomponentsandrelatedclasses,unlessotherwisedocumented,mustbeaccesse