信号量和线程问题
在阅读了关于信号量的内容后,我尝试了这个测试代码,其中创建了两个线程 A 和 B。我的目标是,让线程 A 等待 10 秒,以便线程 B 中的某些操作完成。但是当我稍微改变了线程B中的逻辑,通过引入一个永远不会出来的无限while循环时,线程A也挂起了,程序永远运行。有人可以帮助我如何实现我正在寻找的目标吗?
包包;
import java.util.concurrent.Semaphore;
public class Concurrency {
public int count = 0;
public static void main(String args[]) throws Exception {
final Semaphore sem = new Semaphore(1, true);
Thread thrdA, thrdB;
thrdA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (sem) {
try {
sem.wait(10000);
System.out.println("thread1");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thrdB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (sem) {
System.out.println("thread2");
while(true){
}
}
}
});
thrdA.start();
thrdB.start();
thrdA.join();
thrdB.join();
}
}
After reading about semaphores I tried this test code in which I create two threads A and B. My aim is, let thread A wait for 10 secs for some operation in Thread B to complete. But as I changed the logic in thread B a little , by introducing an infinite while loop which never comes out , the Thread A is also hanging, and the program runs forever. Can anybody help me how to achieve the goal which I am looking for?
package pack;
import java.util.concurrent.Semaphore;
public class Concurrency {
public int count = 0;
public static void main(String args[]) throws Exception {
final Semaphore sem = new Semaphore(1, true);
Thread thrdA, thrdB;
thrdA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (sem) {
try {
sem.wait(10000);
System.out.println("thread1");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thrdB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (sem) {
System.out.println("thread2");
while(true){
}
}
}
});
thrdA.start();
thrdB.start();
thrdA.join();
thrdB.join();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码中有很多问题。
Semaphore
。while(true)
会给 CPU 带来沉重的负载。考虑使用 Thread.sleep 或获取信号量/锁。thread2.run
中执行任何其他调用之前调用thread1.join()
即可。There are many issues in your code.
Semaphore
.while(true)
puts heavy load to the CPU. Consider usingThread.sleep
, or acquiring a semaphore/lock.thread1.join()
before doing any other calls inthread2.run
.Glowcoder 是正确的 - 它会永远运行,因为你的线程没有做任何有用的事情。 thrdA 正在等待信号量,thrdB 永远循环而不执行任何操作 - 它应该在某个时刻对信号量执行某些操作,以便 thrdA 可以跳过等待。
Glowcoder is correct - this runs forever because your threads aren't doing anything useful. thrdA is waiting for the semaphore, thrdB is looping forever doing nothing - it should be doing something to the semaphore at some point so that thrdA can get past the wait.
我一直在网上到处寻找信号量和多线程解决方案,但找不到任何可以阻止线程生成和内存占用的东西。也就是说,如果有 10 亿个数据,而您只需要处理 10 个线程,每个线程一次使用 10000 个数据,我就没有得到更好的解决方案。所以我写了下面的代码来获得正确的答案。
使用这个 semaphore.availablePermits 我能够阻止主线程创建新线程和内存占用。如果您有比这更好的解决方案,请告诉我。我已经测试了 5 亿数据,它显着提高了性能。
i have been looking for semaphore and multithreading solution everywhere on net but was not able to find anything which stops Thread Generation and Memory occupancy. i.e. if there is a billion data and you just need to process 10 threads each using 10000 data at a single time i was not getting better solutions. so i wrote below code to get correct answers.
using this semaphore.availablePermits i was able to hault main thread from creating new threads and memory occupy. if you got any better solution than this kindly let me know. i have testing if for 500 Million data and it significantly inproved the performance.