Concurrent - Semaphore - getQueueLength() & hasQueuedThreads()

原创转载请注明出处:http://agilestyle.iteye.com/blog/2343056

 

getQueueLength()作用是取得等待permits的线程个数

hasQueuedThreads()作用是判断有没有线程在等待permits

这两个方法通常都是在判断当前有没有等待permits的线程信息时使用


Service.java

package org.fool.java.concurrent.semaphore.queue;

import java.util.concurrent.Semaphore;

public class Service {
    private Semaphore semaphore = new Semaphore(1);

    public void testMethod() {
        try {
            semaphore.acquire();

            Thread.sleep(1000);

            System.out.println("Returns an estimate of the number of threads waiting to acquire " + semaphore.getQueueLength());

            System.out.println("Whether any threads are waiting to acquire? " + semaphore.hasQueuedThreads());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}

 

ThreadA.java

package org.fool.java.concurrent.semaphore.queue;

public class ThreadA implements Runnable {
    private Service service;

    public ThreadA(Service service) {
        this.service = service;
    }

    @Override
    public void run() {
        service.testMethod();
    }
}

 

QueueTest.java

package org.fool.java.concurrent.semaphore.queue;

public class QueueTest {
    public static void main(String[] args) {
        Service service = new Service();

        Thread a = new Thread(new ThreadA(service));
        a.start();

        for (int i = 0; i < 5; i++) {
            new Thread(new ThreadA(service)).start();
        }
    }
}

 

Run


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In Verilog, a non-net refers to a variable that is not a wire or a register, but rather a constant or a parameter. Concurrent assignment refers to the assignment of a value to a variable using the &quot;=&quot; operator in a module outside of any procedural blocks (such as always or initial blocks). A concurrent assignment to a non-net is not allowed in Verilog. This is because a non-net does not have a storage element and cannot hold a value assigned to it. Instead, it is typically used as a constant or a parameter that is available for use in the module. To assign a value to a non-net, it should be done within a procedural block using the appropriate assignment operator (such as &quot;&lt;=&quot; for registers or &quot;assign&quot; for wires). Alternatively, the value can be passed as an argument to the module using the parameter keyword. For example: module my_module #(parameter WIDTH = 8) ( input clk, input [WIDTH-1:0] data_in, output [WIDTH-1:0] data_out ); // This is a non-net parameter parameter ADD_VALUE = 5; // This is a register that can be assigned using the &quot;=&quot; operator within an always block reg [WIDTH-1:0] register_data; always @(posedge clk) begin register_data &lt;= data_in + ADD_VALUE; end // This is a wire that can be assigned using the &quot;assign&quot; keyword assign data_out = register_data; endmodule In this example, ADD_VALUE is a non-net parameter that is used in the always block to add a constant value to the input data. The register_data variable is assigned using the &quot;=&quot; operator within the always block. The data_out wire is assigned using the &quot;assign&quot; keyword.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值