java - 关于创建同步机制

标签 java multithreading

Possible Duplicate:
Testing a multithreaded Java class that runs the threads sequentially

请不要将以下问题视为重复问题..!

我开发了一个类,可以让多线程按顺序一次运行一个。该类的claimAccess函数和releaseAccess函数之间的所有应用程序代码一次只会在一个线程中执行。所有其他线程将在队列中等待,直到前一个线程完成。请告知我想通过在 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[])
{

}

请告知我如何在我的主要方法中生成线程来测试上述类..!!请告知

最佳答案

这应该可以帮助您开始...

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();
    }
}

关于java - 关于创建同步机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13784813/

相关文章:

c++ - linux中的pthread_mutex_t是否可重入(如果一个线程试图获取它已经持有的锁,则请求成功)

c# - 线程会自动关闭吗?

java - 应用程序在 sleep 后崩溃

c - 在 sleep() 中退出 pthread 时如何消除错误?

java - 使用 java 解析 xml 字符串和修剪特定标签空间的最佳方法

java 使用逻辑运算符代替 if-else if 进行返回

c++ - 使用 std::thread & std::bind 在成员函数中启动线程

multithreading - WCF ClientBase线程安全吗?

Java 客户端和服务器 - 多个请求

java - 在 OpenGL ES (Android) 中使用带有纹理的索引缓冲区有什么意义吗?