- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
因此,我尝试使用两个自定义线程和一个预定的线程执行器来实现从 Java 客户端到 Go 服务器的基本异步消息传递协议(protocol)。因此,我从 Java 客户端向 Go 服务器发起单个 TCP 套接字,并在两侧设置读取和写入缓冲区。
Writer线程将每个请求对应的唯一ID保存到Redis服务器中,并在TCP套接字上使用PrintWriter写入请求。
作家线程
public class WithdrawRequestWriter extends Thread {
private static final int START_ACCOUNT_NO = 1;
private static final int END_ACCOUNT_NO = 2_000;
private static final int AMOUNT_TO_WITHDRAW = 100;
private PrintWriter writer;
private Jedis jedis;
public WithdrawRequestWriter(PrintWriter writer, Jedis jedis) {
this.writer = writer;
this.jedis = jedis;
}
@Override
public void run() {
int i = 0;
for (int accountNo = START_ACCOUNT_NO; accountNo <= END_ACCOUNT_NO; accountNo++) {
String uniqueId = UUID.randomUUID().toString();
if (jedis.set(uniqueId, String.valueOf(accountNo), "NX", "EX", 10) != null) {
writer.write(uniqueId + " " + accountNo + " " + AMOUNT_TO_WITHDRAW + "\n");
writer.flush();
} else {
System.err.println("Setting of UUID in redis failed: " + uniqueId);
}
}
}
}
同样,读取器线程等待服务器的响应。一旦收到,它就会与 Redis 中的唯一 ID 匹配,将它们标记为完成(成功/失败)。
读者话题
public class WithdrawRequestReader extends Thread {
private BufferedReader reader;
private Jedis jedis;
public WithdrawRequestReader(BufferedReader reader, Jedis jedis) {
this.reader = reader;
this.jedis = jedis;
}
@Override
public void run() {
String response;
while (true) {
try {
if ((response = reader.readLine()) != null) {
String[] tokenizedResponse = response.split(" ");
System.out.println("Response is: " + response);
String value = jedis.get(tokenizedResponse[0]);
if (value != null) {
System.out.println("Request Token: " + tokenizedResponse[0]
+ " | Status: " + tokenizedResponse[1]
+ " | Account: " + tokenizedResponse[2]
+ " | Amount: " + tokenizedResponse[3]
+ " | Remaining Balance: " + tokenizedResponse[4]
+ " | ResponeStr: " + tokenizedResponse[5]);
}
} else {
System.out.println("Waiting for response...");
}
} catch (IOException ex) {
ex.printStackTrace();
System.exit(3);
}
}
}
}
主线程
public class TestClient {
private static final String HOSTNAME = "localhost";
private static final int PORT = 12410;
public static void main(String[] args) throws IOException {
Socket socket = new Socket(HOSTNAME, PORT);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Jedis jedis = new Jedis("localhost");
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
// ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
WithdrawRequestReader readerThread = new WithdrawRequestReader(reader, jedis);
WithdrawRequestWriter writerThread = new WithdrawRequestWriter(writer, jedis);
scheduledExecutorService.schedule(writerThread, 0, TimeUnit.MILLISECONDS);
scheduledExecutorService.schedule(readerThread, 0, TimeUnit.MILLISECONDS);
}
}
当我使用 singlePoolThreadExecutor 时,任务按顺序执行(如预期),一切都很顺利。但是,当我将 threadPoolExecutor 与两个线程一起使用时,经过几次读写后,它们无限期地进入等待状态。我通过在线可用的免费工具检查了是否存在僵局,但没有得到任何帮助。下面是当它们被卡住时的线程转储。
线程转储
Full thread dump Java HotSpot(TM) 64-Bit Server VM (9.0.4+11 mixed mode):
"DestroyJavaVM" #16 prio=5 os_prio=31 tid=0x00007f974e06d000 nid=0x2503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"pool-1-thread-2" #15 prio=5 os_prio=31 tid=0x00007f974e06c000 nid=0x6003 waiting on condition [0x000070000c819000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@9.0.4/Native Method)
- parking to wait for <0x0000000747cf64b0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(java.base@9.0.4/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(java.base@9.0.4/AbstractQueuedSynchronizer.java:2062)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(java.base@9.0.4/ScheduledThreadPoolExecutor.java:1119)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(java.base@9.0.4/ScheduledThreadPoolExecutor.java:848)
at java.util.concurrent.ThreadPoolExecutor.getTask(java.base@9.0.4/ThreadPoolExecutor.java:1092)
at java.util.concurrent.ThreadPoolExecutor.runWorker(java.base@9.0.4/ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(java.base@9.0.4/ThreadPoolExecutor.java:641)
at java.lang.Thread.run(java.base@9.0.4/Thread.java:844)
"pool-1-thread-1" #14 prio=5 os_prio=31 tid=0x00007f974e8a9800 nid=0x5e03 waiting on condition [0x000070000c716000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@9.0.4/Native Method)
- parking to wait for <0x0000000747cf64b0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(java.base@9.0.4/LockSupport.java:194)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(java.base@9.0.4/AbstractQueuedSynchronizer.java:2062)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(java.base@9.0.4/ScheduledThreadPoolExecutor.java:1119)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(java.base@9.0.4/ScheduledThreadPoolExecutor.java:848)
at java.util.concurrent.ThreadPoolExecutor.getTask(java.base@9.0.4/ThreadPoolExecutor.java:1092)
at java.util.concurrent.ThreadPoolExecutor.runWorker(java.base@9.0.4/ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(java.base@9.0.4/ThreadPoolExecutor.java:641)
at java.lang.Thread.run(java.base@9.0.4/Thread.java:844)
"Service Thread" #11 daemon prio=9 os_prio=31 tid=0x00007f974d827000 nid=0xa003 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=31 tid=0x00007f974e078800 nid=0x5903 runnable [0x000070000c40d000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(java.base@9.0.4/Native Method)
at java.net.SocketInputStream.socketRead(java.base@9.0.4/SocketInputStream.java:116)
at java.net.SocketInputStream.read(java.base@9.0.4/SocketInputStream.java:171)
at java.net.SocketInputStream.read(java.base@9.0.4/SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(java.base@9.0.4/StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(java.base@9.0.4/StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(java.base@9.0.4/StreamDecoder.java:178)
- locked <0x0000000747d9ea78> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(java.base@9.0.4/InputStreamReader.java:185)
at java.io.BufferedReader.fill(java.base@9.0.4/BufferedReader.java:161)
at java.io.BufferedReader.readLine(java.base@9.0.4/BufferedReader.java:326)
- locked <0x0000000747d9ea78> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(java.base@9.0.4/BufferedReader.java:392)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
"Common-Cleaner" #9 daemon prio=8 os_prio=31 tid=0x00007f974e00a000 nid=0xa203 in Object.wait() [0x000070000c30a000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(java.base@9.0.4/Native Method)
- waiting on <0x0000000747f37610> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@9.0.4/ReferenceQueue.java:151)
- waiting to re-lock in wait() <0x0000000747f37610> (a java.lang.ref.ReferenceQueue$Lock)
at jdk.internal.ref.CleanerImpl.run(java.base@9.0.4/CleanerImpl.java:148)
at java.lang.Thread.run(java.base@9.0.4/Thread.java:844)
at jdk.internal.misc.InnocuousThread.run(java.base@9.0.4/InnocuousThread.java:122)
"Sweeper thread" #8 daemon prio=9 os_prio=31 tid=0x00007f974d81c000 nid=0x5803 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007f974d81b000 nid=0xa503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task
"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007f974e005000 nid=0xa603 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task
"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007f974d81a800 nid=0xa803 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
No compile task
"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007f974e01a800 nid=0x5603 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007f974d041800 nid=0x4003 in Object.wait() [0x000070000bc75000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(java.base@9.0.4/Native Method)
- waiting on <0x0000000747f0d078> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@9.0.4/ReferenceQueue.java:151)
- waiting to re-lock in wait() <0x0000000747f0d078> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(java.base@9.0.4/ReferenceQueue.java:172)
at java.lang.ref.Finalizer$FinalizerThread.run(java.base@9.0.4/Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007f974d040800 nid=0x4103 waiting on condition [0x000070000bb72000]
java.lang.Thread.State: RUNNABLE
at java.lang.ref.Reference.waitForReferencePendingList(java.base@9.0.4/Native Method)
at java.lang.ref.Reference.processPendingReferences(java.base@9.0.4/Reference.java:174)
at java.lang.ref.Reference.access$000(java.base@9.0.4/Reference.java:44)
at java.lang.ref.Reference$ReferenceHandler.run(java.base@9.0.4/Reference.java:138)
"VM Thread" os_prio=31 tid=0x00007f974e884000 nid=0x4303 runnable
"GC Thread#0" os_prio=31 tid=0x00007f974e80d800 nid=0x2a03 runnable
"GC Thread#1" os_prio=31 tid=0x00007f974e80e800 nid=0x5303 runnable
"GC Thread#2" os_prio=31 tid=0x00007f974d80c800 nid=0x2b03 runnable
"GC Thread#3" os_prio=31 tid=0x00007f974d000800 nid=0x2c03 runnable
"G1 Main Marker" os_prio=31 tid=0x00007f974e835800 nid=0x4803 runnable
"G1 Marker#0" os_prio=31 tid=0x00007f974d80d800 nid=0x4503 runnable
"G1 Refine#0" os_prio=31 tid=0x00007f974e80f800 nid=0x4a03 runnable
"G1 Refine#1" os_prio=31 tid=0x00007f974d80d000 nid=0x4c03 runnable
"G1 Refine#2" os_prio=31 tid=0x00007f974e80f000 nid=0x4e03 runnable
"G1 Refine#3" os_prio=31 tid=0x00007f974e806800 nid=0x5003 runnable
"G1 Young RemSet Sampling" os_prio=31 tid=0x00007f974e810800 nid=0x2f03 runnable
"VM Periodic Task Thread" os_prio=31 tid=0x00007f974e079800 nid=0x5c03 waiting on condition
JNI global references: 421
Heap
garbage-first heap total 131072K, used 10240K [0x0000000740000000, 0x0000000740100400, 0x00000007c0000000)
region size 1024K, 11 young (11264K), 0 survivors (0K)
Metaspace used 8824K, capacity 9274K, committed 9472K, reserved 1058816K
class space used 832K, capacity 927K, committed 1024K, reserved 1048576K
我相信Go服务器不是问题,否则单线程执行器也应该卡住,但事实并非如此。可能我遇到了一些僵局,但无法找出确切的原因。有人可以帮忙吗?如果您还想要什么,请告诉我。
最佳答案
代码的套接字和流部分似乎是正确的。并行使用套接字的输入和输出流是正确的,因为它们是独立的流。
问题出在 Jedis 组件上。 Jedis 不是线程安全的。 Jedis 文档说:
You shouldn't use the same instance from different threads because you'll have strange errors. And sometimes creating lots of Jedis instances is not good enough because it means lots of sockets and connections, which leads to strange errors as well. A single Jedis instance is not threadsafe!...
需要重写代码,让每个线程都有一个私有(private)的 Jedis 实例。
关于Java 调度执行器线程进入等待状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50451219/
我试图让脚本暂停大约 1 秒,然后继续执行脚本,但我似乎无法弄清楚如何做。这是我的代码: function hello() { alert("Hi!") //I need about a 1
wait() 和 wait(timeout) 之间有什么区别。无论如何 wait() 需要等待通知调用,但为什么我们有 wait(timeout)? 那么 sleep(timeout) 和 wait(
我需要做什么: 我有一个带有文件输入和隐藏文本输入的上传表单。用户上传图像,图像被操作,然后发送到远程服务器进行处理,这需要几秒钟,然后远程服务器将最终的图像发送回家庭服务器,并保存在新文件夹中。 J
大家好,我正在使用 Visual C++ 2010,尝试使用 Winsock 编写服务器/客户端应用程序...我不确定为什么,但有时服务器会在 listen() 函数处等待,有时会在 accept 处
任务描述 我为我的 Angular 应用程序实现了 CRSF 保护。服务器检查 crsf token 是否位于请求的 header “X-CSRF-TOKEN”中。如果不是,它会发送一个 HTTP 响
我想做这个例子https://stackoverflow.com/a/33585993/1973680同步。 这是正确的实现方式吗? let times= async (n,f)=>{
我如何将 while 循环延迟到 1 秒间隔,而不会将其运行的整个代码/计算机的速度减慢到一秒延迟(只是一个小循环)。 最佳答案 Thread.sleep(1000); // do nothing f
我知道这是一个重复的问题。但是我无法通过解释来理解。我想用一个很好的例子来清楚地理解它。任何人都可以帮忙吗。 “为什么我们从同步上下文中调用 wait()、notify() 方法”。 最佳答案 当我们
我有一个 click 事件,该事件是第一次从另一个地方自动触发的。我的问题是它运行得太快,因为所需的变量仍在由 Flash 和 Web 服务定义。所以现在我有: (function ($) {
我有如下功能 function async populateInventories(custID){ this.inventories = await this.inventoryServic
我一直对“然后”不被等待的行为感到困扰,我明白其原因。然而,我仍然需要绕过它。这是我的用例。 doWork(family) { return doWork1(family)
我想我理解异步背后的想法,返回一个Future,但是我不清楚异步在一个非常基本的层面上如何表现。据我了解,它不会自动在程序中创建异步行为。例如: import 'dart:async'; main()
我正在制作一个使用异步的Flutter应用程序,但它的工作方式不像我对它的了解。所以我对异步和在 Dart 中等待有一些疑问。这是一个例子: Future someFunction() async {
我在 main.tf 中创建资源组和 vNet,并在同一文件中引用模块。问题是,模块无法从模块访问这些资源。相关代码(删除了大部分代码,只留下相关部分): main.tf: module "worke
我的代码的问题是,当代码第一次运行时,我试图获取的 dom 元素并不总是存在,如果它不存在,那么永远不会做出 promise 。 我是否可以等到 promise 做出后再尝试实现它? 我希望我的最后一
所以,过去几天我一直在研究这段代码,并尝试实现回调/等待/任何需要的东西,但没有成功。 问题是,我如何等待响应,直到我得到两个函数的回调? (以及我将如何实现) 简而言之,我想做的是: POST 发生
谁能帮我理解这一点吗? 如果我们有一个类: public class Sample{ public synchronized method1(){ //Line1 .... wait();
这是我编写的代码,用于测试 wait() 和 notify() 的工作。现在我有很多疑问。 class A extends Thread { public void run() { try
我有以下代码由于语法错误而无法运行(在异步函数外等待) 如何使用 await 定义变量并将其导出? 当我这样定义一个变量并从其他文件导入它时,该变量是只创建一次(第一次读取文件时?)还是每次导入时都创
一个简单的线程程序,其中写入器将内容放入堆栈,读取器从堆栈中弹出。 java.util.Stack; import java.util.concurrent.ExecutorService; impo
我是一名优秀的程序员,十分优秀!