Java 关于创建同步机制
可能重复:Java 关于创建同步机制,java,multithreading,Java,Multithreading,可能重复: 请不要将下面的问题视为重复问题 我开发了一个类,可以让多个线程按顺序运行,一次一个。此类的claimeraccess函数和release Access函数之间的所有应用程序代码一次只能在一个线程中执行。所有其他线程将在队列中等待,直到前一个线程完成。请建议我想通过在main()方法本身中编写一段代码来测试我的类 import java.util.ArrayList; import java.util.List; public class AccessGate { prot
请不要将下面的问题视为重复问题 我开发了一个类,可以让多个线程按顺序运行,一次一个。此类的claimeraccess函数和release Access函数之间的所有应用程序代码一次只能在一个线程中执行。所有其他线程将在队列中等待,直到前一个线程完成。请建议我想通过在main()方法本身中编写一段代码来测试我的类
import java.util.ArrayList;
import java.util.List;
public class AccessGate {
protected boolean shouldWait = false;
protected final List waitThreadQueue = new ArrayList();
/**
* For a thread to determine if it should wait. It it is, the thread will
* wait until notified.
*
*/
public void claimAccess() {
final Thread thread = getWaitThread();
if (thread != null) {
// let the thread wait untill notified
synchronized (thread) {
try {
thread.wait();
} catch (InterruptedException exp) {
}
}
}
}
/**
* For a thread to determine if it should wait. It it is, the thread will be
* put into the waitThreadQueue to wait.
*
*/
private synchronized Thread getWaitThread() {
Thread thread = null;
if (shouldWait || !waitThreadQueue.isEmpty()) {
thread = Thread.currentThread();
waitThreadQueue.add(thread);
}
shouldWait = true;
return thread;
}
/**
* Release the thread in the first position of the waitThreadQueue.
*
*/
public synchronized void releaseAccess() {
if (waitThreadQueue.isEmpty()) {
shouldWait = false;
} else {
shouldWait = true;
// give the claimAccess function a little time to complete
try {
Thread.sleep(10);
} catch (InterruptedException exp) {
}
// release the waiting thread
final Thread thread = (Thread) waitThreadQueue.remove(0);
synchronized (thread) {
thread.notifyAll();
}
}
}
}
现在我的主要方法是
public static void main (String args[])
{
}
请告知我如何在我的main方法中生成thr线程来测试上述类。。!!请告知这将让您开始
public static void main (String args[])
{
AccessGate gate = new AccessGate();
// create as many threads as you like
Thread t1 = new MyThread(gate);
Thread t2 = new MyThread(gate);
// start all the threads you created
t1.start();
t2.start();
}
class MyThread extends Thread {
AccessGate gate;
public MyThread(AccessGate g) {
gate = g;
}
public void run() {
gate.claimAccess();
// Do something or print something.
// Could output several statements.
// Why not do a sleep as well to see if other threads interrupt
// this code section.
gate.releaseAccess();
}
}
考虑使用
Executors.newSingleThreadExecutor()
。这是一个线程池,只有一个线程执行任务。只有在第一个任务完成后,下一个任务才会开始执行:
Executor executor = Executors.newSingleThreadExecutor();
Future<String> future1 = executor.submit(new Callable<String>() {
@Override
String call() throws Exception {
// my first task
}
});
Future<String> future2 = executor.submit(new Callable<String>() {
@Override
String call() throws Exception {
// my second task
}
});
...
Executor Executor=Executors.newSingleThreadExecutor();
Future future1=执行者。提交(新的可调用(){
@凌驾
字符串调用()引发异常{
//我的第一项任务
}
});
Future future2=执行者。提交(新的可调用(){
@凌驾
字符串调用()引发异常{
//我的第二项任务
}
});
...
您可以通过未来的API检索任务执行的结果,还可以跟踪每个作业的状态。考虑使用执行器。newSingleThreadExecutor()@Hoaz非常感谢,请发布完整的代码,以便我能够理解更多,谢谢请告诉我如何使用执行器转换上述代码,如果您能像我一样发布完整的代码,我将非常感激。提前感谢您不需要任何门,只需将您的任务(可调用实例)提交给executor,它们将依次执行。非常感谢,但是我可以通过你建议的方法从主方法访问这个,但是我试图通过另一种方法完全通过重入锁来访问上面的类访问门,请对此提出建议。我不确定我是否正确理解你,你能再次解释我吗?我上面提供的代码已经包含了所有同步块,可以逐个执行多个任务。你所需要的就是提交任务,executor将负责运行它们。我想用上面所示的方法创建访问门类,还有其他方法通过reentrant lock实现吗?请你发布完整的代码,因为我已经创建了访问门