Java 不使用同步线程的线程安全队列
我必须创建一个程序,在给定N个线程的情况下,这些线程可以从队列中插入或删除元素,但线程访问队列有以下条件:Java 不使用同步线程的线程安全队列,java,multithreading,Java,Multithreading,我必须创建一个程序,在给定N个线程的情况下,这些线程可以从队列中插入或删除元素,但线程访问队列有以下条件: 如果只有一个线程尝试插入或删除一个元素,它将能够 如果两个或多个线程同时尝试,则其中一个线程将能够尝试,而下一个线程将在第一个线程完成时执行其操作 我使用同步块制作,就像这样: import java.util.ArrayList; import java.util.Random; public class EditorThread extends Thread { sta
- 如果只有一个线程尝试插入或删除一个元素,它将能够李>
- 如果两个或多个线程同时尝试,则其中一个线程将能够尝试,而下一个线程将在第一个线程完成时执行其操作
import java.util.ArrayList;
import java.util.Random;
public class EditorThread extends Thread {
static int N = 10; // number of threads
static queue Q = new queue(); // shared queue
private int number; //number of the thread
public EditorThread(int n) {
number = n;
}
@Override
public void run() {
Random r = new Random();
while (true) {
int t = r.nextInt(2);
if (t == 1) {
int value = Q.get();
if (value == -1) {
System.out.println("The Thread " + number + " couldnt get any element (empty queue)");
}
else {
System.out.println("The Thread " + number + " got the element " + value );
}
}
else {
int n = r.nextInt(100);
Q.put(n);
System.out.println("The Thread " + number + " inserted the element " + n);
}
}
}
public static void main(String[] args) {
for (int i = 0; i < N; i++) {
Thread t = new EditorThread(i);
t.start();
}
}
}
class queue {
node head;
node tail;
queue() {
head = tail = null;
}
public synchronized int get() {
if (head == null)
return -1;
int r = head.value;
if (head != tail)
head = head.next;
else
head = tail = null;
return r;
}
public synchronized void put(int i) {
node n = new node(i);
if (head == null)
head = tail = n;
else {
tail.next = n;
tail = n;
}
}
}
class node {
int value;
node next;
public node(int value) {
this.value = value;
}
}
import java.util.ArrayList;
导入java.util.Random;
公共类EditorRead扩展线程{
static int N=10;//线程数
静态队列Q=新队列();//共享队列
private int number;//线程的编号
公共编辑阅读(int n){
数字=n;
}
@凌驾
公开募捐{
随机r=新随机();
while(true){
int t=r.nextInt(2);
如果(t==1){
int value=Q.get();
如果(值==-1){
System.out.println(“线程”+number+“无法获取任何元素(空队列)”);
}
否则{
System.out.println(“线程“+number+”得到元素“+value”);
}
}
否则{
int n=r.nextInt(100);
Q.put(n);
System.out.println(“线程”+number+”插入元素“+n”);
}
}
}
公共静态void main(字符串[]args){
对于(int i=0;i
runvoid很简单,它只是在插入或删除元素时永远循环
我的问题是,如何在不使用synchronized的情况下遵循该条件
在没有同步块的情况下,如何保证互斥
编辑:我不能使用与同步类似的东西(就像锁)否,是的
基本上,您需要使用某种形式的同步来实现这一点。没有你自己是不可能做到的
但是,java.util.concurrent
包中有一些类,它们提供了您所需要的行为,同时尽可能减少锁定和同步成本
例如LinkedBlockingQueue
如果你真的想了解这些东西是如何工作的,尽管你也应该阅读非阻塞算法。wiki页面是一个良好的开端。一般来说,很多非常聪明的人都知道自己在做什么,但他们都在使用并发包。线程是很难得到正确的
不,是的
基本上,您需要使用某种形式的同步来实现这一点。没有你自己是不可能做到的
但是,java.util.concurrent
包中有一些类,它们提供了您所需要的行为,同时尽可能减少锁定和同步成本
例如LinkedBlockingQueue
如果你真的想了解这些东西是如何工作的,尽管你也应该阅读非阻塞算法。wiki页面是一个良好的开端。一般来说,很多非常聪明的人都知道自己在做什么,但他们都在使用并发包。线程是很难得到正确的
是否有充分的理由不使用像
LinkedBlockingQueue这样的实现?根据约定,这些是线程安全的。是的,我这样做是为了学习您是否能够使用原子类*
类?无论您实现什么,都将是对synchronized method mate的再创造(在某种程度上),您可能想读一个好问题:我不能使用原子类,但可以使用一些技巧,比如使线程保持静态,因此,一个线程可以停止另一个线程(但我不能安全地使用它),有没有理由不使用像LinkedBlockingQueue
这样的实现?根据约定,这些是线程安全的。是的,我这样做是为了学习您是否能够使用原子类*
类?无论您实现什么,都将是对synchronized method mate的再创造(在某种程度上),您可能想读一个好问题:我不能使用原子类,但可以使用一些技巧,比如使线程保持静态,因此一个线程可以阻止另一个线程(但我无法安全地使用此线程)