java里的another,我可以從不同的線程訪問Java中的另一個線程資源嗎?

I have a situation where I want to access threads in Waiting or Blocked state and destroy their connections to an external Server. Is this possible without making a global variable that stores those connections? Can I access those connection objects from another Thread in Java?

我有一種情況,我想訪問處於等待或阻塞狀態的線程並破壞它們與外部服務器的連接。如果沒有創建存儲這些連接的全局變量,這可能嗎?我可以從Java中的另一個線程訪問這些連接對象嗎?

To be more specific:

更具體:

I had connections with a RabbitMQ Server which throttles connections when it reaches a particular memory threshold which in my case it has reached. So, the client hangs waiting for RabbitMQ server to unblock it which would never happen in my use case. So, I want to close those particular connections. I have tried sending interrupts to those threads but it seems like those threads are not recognizing interrupts and remain hanged indefinitely.

我與RabbitMQ服務器建立了連接,當它到達特定的內存閾值時會限制連接,在我的情況下它達到了這個閾值。因此,客戶端掛起等待RabbitMQ服務器解除阻塞,這在我的用例中永遠不會發生。所以,我想關閉那些特定的聯系。我曾嘗試向這些線程發送中斷,但似乎這些線程沒有識別中斷並且無限期地被掛起。

1 个解决方案

#1

1

Resources do not belong to threads, Java doesn't care which thread has a reference to a Socket object for example.

資源不屬於線程,Java並不關心哪個線程具有對Socket對象的引用。

You'll need a central ConcurrentHashMap or something like that to make your connection objects visible to the outside. Or you can interrupt threads and let them kill the connection, since almost all things wait / blocking do throw InterruptedExceptions when you do so.

您需要一個中央ConcurrentHashMap

或類似的東西,以使您的連接對象對外部可見。或者您可以中斷線程並讓它們終止連接,因為幾乎所有等待/阻塞的事情都會在您執行此操作時拋出InterruptedExceptions。

So in case you know which thread it is, it's typically something like

所以,如果你知道它是哪個線程,它通常是這樣的

Socket socket = connectionMap.get(thread);

socket.close();

or

thread.interrupt();

which would go together with thread code like

這將與線程代碼一起使用

Thread myThread = Thread.currentThread();

connect();

while(!myThread.isInterrupted()) {

try {

doStuff();

} catch (InterruptedException e) {

myThread.interrupt(); // exception does not set interrupted state

}

}

disconnect();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ,可以通过创建一个线程类来执行另一个线程类,并且控制另一个线程类的暂停和启动。 首先,需要创建一个线程类,可以继承 `Thread` 类或实现 `Runnable` 接口。这个线程类将执行另一个线程类。 ```java class ThreadClass extends Thread { private AnotherThreadClass anotherThread; public ThreadClass(AnotherThreadClass anotherThread) { this.anotherThread = anotherThread; } @Override public void run() { // 执行另一个线程类 anotherThread.start(); } public void pauseAnotherThread() { // 暂停另一个线程类 anotherThread.pauseThread(); } public void resumeAnotherThread() { // 恢复另一个线程类 anotherThread.resumeThread(); } } class AnotherThreadClass extends Thread { private boolean isPaused; public AnotherThreadClass() { this.isPaused = false; } @Override public synchronized void run() { while (true) { if (!isPaused) { // 执行线程逻辑 } else { // 线程暂停 try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public synchronized void pauseThread() { isPaused = true; } public synchronized void resumeThread() { isPaused = false; notify(); } } ``` 在上面的示例,`ThreadClass` 继承了 `Thread` 类,并且在其 `run()` 方法执行了另一个线程类 `AnotherThreadClass`。`AnotherThreadClass` 继承了 `Thread` 类,其包含了一个标志位 `isPaused`,用于控制线程的暂停和恢复。 `AnotherThreadClass` 的 `run()` 方法,通过检查 `isPaused` 标志位来判断是否需要暂停线程。如果需要暂停,线程将调用 `wait()` 方法,使线程进入等待状态。当调用 `resumeThread()` 方法后,线程将恢复执行,并通过 `notify()` 方法唤醒等待的线程。 使用时,可以创建一个 `ThreadClass` 对象,并调用其 `start()` 方法来执行另一个线程类。 ```java AnotherThreadClass anotherThread = new AnotherThreadClass(); ThreadClass thread = new ThreadClass(anotherThread); thread.start(); ``` 可以通过调用 `thread.pauseAnotherThread()` 方法来暂停另一个线程类的执行,调用 `thread.resumeAnotherThread()` 方法来恢复另一个线程类的执行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值