Tag: multithreading

为什么主要顺序中没有呼叫?

我正在阅读一本关于线程/同步的简单示例,该书声称使用synchronized将允许在同一实例上调用一个线程来访问该方法。 它确实按照承诺进行了序列化,但似乎在下面的Synch main方法中创建的第三个Caller大约9/10倍出现在第二个之前。 此代码是示例代码,显示没有同步方法的问题。 class CallMe { void call(String msg) { System.out.print(“[” + msg); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println(“CallMe Interrupted”); } System.out.println(“]”); } } class Caller implements Runnable { String msg; CallMe target; Thread t; public Caller (CallMe target, String msg) { this.target = target; this.msg = msg; t = new […]

如何正确处理来自ListenableFuture番石榴的exception?

我有一个库,我已经为我们的客户提供了两种方法,sync和async。 他们可以调用他们认为适合他们目的的任何方法。 executeSynchronous() – 等到我有结果,返回结果。 executeAsynchronous() – 立即返回一个Future,如果需要,可以在其他事情完成后处理。 它们将传递具有用户标识的DataKey对象。 我们将根据用户ID确定调用哪台机器。 因此,我们将使用AsyncRestTemplate对URL进行http调用,然后根据它是否成功将响应发送给它们。 以下是我的界面: public interface Client { // for synchronous public DataResponse executeSync(final DataKey key); // for asynchronous public Future executeAsync(final DataKey key); } 以下是我的实施: public class DataClient implements IClient { // does this have to be final? private final AsyncRestTemplate restTemplate = new AsyncRestTemplate(); @Override […]

主线程exception上的Android Bump Api网络

首先,我对Android和JAVA世界都很陌生(来自C / C ++ / Objective-C)。 我正在尝试集成Android bump API(3.0,最新版本),但我遇到了麻烦。 我复制了这个例子,它在Android 2.2下工作正常,碰撞服务正确启动,但对于Android 3.0及其上层它不起作用。 在加载我的活动时,我有一个exception(主线程上的网络),我知道这个exception以及如何避免它,但在这种情况下,Bump表示他们在自己的线程中运行他们的API所以我不这样做真的知道我为什么得到它。 他们说你不需要运行一个线程或任务。 以下是我的活动示例 public class BumpActivity extends Activity { private IBumpAPI api; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.bump); bindService(new Intent(IBumpAPI.class.getName()), connection, Context.BIND_AUTO_CREATE); IntentFilter filter = new IntentFilter(); filter.addAction(BumpAPIIntents.CHANNEL_CONFIRMED); filter.addAction(BumpAPIIntents.DATA_RECEIVED); filter.addAction(BumpAPIIntents.NOT_MATCHED); filter.addAction(BumpAPIIntents.MATCHED); filter.addAction(BumpAPIIntents.CONNECTED); registerReceiver(receiver, filter); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); […]

Android:如何从工作线程到服务进行通信

我已经创建了一个服务类和一个在一个单独的线程中执行的worker类。 我想在它们之间建立通信,因此工作人员可以将某些状态发送回服务。 我试图将我的工作Thread转换为HandlerThread并在服务端设置一个Handler ,但后来我不知道如何实际从工作人员发送消息。 看起来我无法理解这个概念。 这是我的课程(没有沟通逻辑): 服务类 package com.example.app; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class ConnectionService extends Service { protected ConnectionWorker thread; @Override public void onCreate() { super.onCreate(); // Creating a connection worker thread instance. // Not starting it yet. this.thread = new ConnectionWorker(); Log.d(this.getClass().getName(), “Service created”); } @Override public int […]

为什么java无法从死锁中恢复?

我正在阅读Java Concurrency in Practice一书,这里有关于死锁的内容。 JVM无法从死锁中恢复,只有摆脱死锁的方法是重启服务器。 它还提到JVM使用图搜索,其中Thread充当图节点,并且两个线程A和B之间的边缘被定义为线程A正在等待线程B已经拥有的资源上的锁定。该图是有针对性的,如果有任何周期这个图,然后有死锁 现在我的问题是,如果JVM知道存在死锁,那为什么不杀死一个线程并让其他线程继续? 这背后是否有任何具体原因或我的问题本身是基于错误的结论? 请告诉我您对此的看法。 提前致谢!!!

为什么在一个线程迭代(使用Iterator)和其他线程修改非线程安全的ArrayList的相同副本时没有ConcurrentModificationException

一些背景: 当使用Iterator一个集合时,可能会有java.util.ConcurrentModificationException因为在创建Iterator对象的时候,会捕获集合或ArrayList的修改计数( modCount ),并在每次迭代时使用Iterator.next()它检查modCount是否已更改,如果是,则抛出java.util.ConcurrentModificationException 。 在创建迭代器对象时(来自ArrayList的Iterator实现): int expectedModCount = modCount; 下面的方法在Iterator.next()调用,它抛出exception(来自ArrayList的Iterator实现): final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } 我可以使用下面的代码很好地重现它: List stringList = new ArrayList(); stringList.add(“a”); stringList.add(“b”); stringList.add(“c”); Iterator iterator = stringList.iterator(); System.out.println(“Got iterator object”); while (iterator.hasNext()) { String player = iterator.next(); player.toString(); System.out.println(“UpperCase: ” + player.toUpperCase()); iterator.remove(); stringList.add(“a1”); //This is […]

Java背景线程

我想知道如何最好地实现后台来执行某些任务。 根据任务中的某些条件,它将结束并返回调用者的状态。 此外,当后台线程正在运行时,它不应该阻止调用程序线程等待其完成。 我尝试过FutureTask但它同步完成所有事情。 请极客帮我。

从主线程hibernate是抛出InterruptedException

我有执行的主线程产生新线程。 在main()的主要执行线程中,我调用Thread.sleep() 。 我什么时候得到Unhandledexception类型InterruptedException ? 我不确定为什么我会这样做。 我认为这是因为我需要对主线程的引用,所以我继续通过Thread.currentThread()引用它。 这不是让线程睡觉的方法吗? 我需要做的是让主线程等待/睡眠/延迟,直到它再次需要工作。

在java中调用Thread实例上的wait()

如果我们在Thread类的实例上调用wait()方法会发生什么。 Thread t1 = new MyThread(); t1.wait(); 我的线程t1的状态是什么?

Java中的单例设计模式

Singleton中的Double Lock检查通常写为: public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { //1 if (instance == null) //2 instance = new Singleton(); //3 } } return instance; //4 } 在上面的代码中,假设有十个线程正在调用此方法,所有这些线程都超过了第一个if条件,然后一个线程进入synchronized块并创建实例。 即使创建了实例,它们也需要等待并顺序通过synchronized块,剩下的9个线程将逐个出现。 我希望只要任何线程创建Singleton实例,所有其他线程都不应该等待。 告诉我是否有一些解决方案?