首页 \ 问答 \ 为什么Executors创建Executor使用LinkedBlockingQueue而不是ConcurrentLinkedQueue(Why Executors create Executor use LinkedBlockingQueue instead of ConcurrentLinkedQueue)

为什么Executors创建Executor使用LinkedBlockingQueue而不是ConcurrentLinkedQueue(Why Executors create Executor use LinkedBlockingQueue instead of ConcurrentLinkedQueue)

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

使用LinkedBlockingQueue,但ConcurrentLinkedQueue应该更高效,哪些是无阻塞和无锁?


public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

Use LinkedBlockingQueue, But does ConcurrentLinkedQueue should be more efficient , which is no-blocking and lock free?


原文:https://stackoverflow.com/questions/32265928
更新时间:2023-08-19 16:08

最满意答案

如果你不喜欢循环,请使用递归:)

 public static void test1() {
    class Chk {
        boolean c(int [] b, int val, int pos) {
            if (pos >= b.length) {
                return true;
            }
            if (b[pos] != val) {
                return false;
            }
            return c(b, val, pos + 1);
        }
    }
    Chk test = new Chk();

    System.out.println(test.c(new int [] {0xff, 0xff}, 0xff, 0));

    System.out.println(test.c(new int [] {0xff, 0xff, 0xff, 0xfe}, 0xff, 0));

    System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0xff, 0));

    System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0x01, 0));
}

If you don't like looping, use recursion :)

 public static void test1() {
    class Chk {
        boolean c(int [] b, int val, int pos) {
            if (pos >= b.length) {
                return true;
            }
            if (b[pos] != val) {
                return false;
            }
            return c(b, val, pos + 1);
        }
    }
    Chk test = new Chk();

    System.out.println(test.c(new int [] {0xff, 0xff}, 0xff, 0));

    System.out.println(test.c(new int [] {0xff, 0xff, 0xff, 0xfe}, 0xff, 0));

    System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0xff, 0));

    System.out.println(test.c(new int [] {0x01, 0x01, 0x01, 0x01}, 0x01, 0));
}

相关问答

更多
  • 与0xFF整数只留下最低有效字节。 例如,要获得short s的第一个字节,可以写s & 0xFF 。 这通常被称为“掩蔽”。 如果byte1是单字节类型(如uint8_t )或者已经小于256(因此除了最低有效字节以外全部为零),则不需要屏蔽掉高位,因为它们已经为零。 当您使用签名类型时,请参阅 下面的tristopia PatrickSchlüter 的答案。 在进行按位操作时,我建议只使用无符号类型。 Anding an integer with 0xFF leaves only the least ...
  • 如果你不喜欢循环,请使用递归:) public static void test1() { class Chk { boolean c(int [] b, int val, int pos) { if (pos >= b.length) { return true; } if (b[pos] != val) { return false; ...
  • MemoryStream.GetBuffer()返回MemoryStream用于存储数据的内部字节数组。 最初它填充0,并通过Write填充到流长度。 很可能你想使用MemoryStream.ToArray()而不是将截断的缓冲区的副本返回到实际长度。 MemoryStream.GetBuffer() returns internal byte array that MemoryStream uses to store data. Initially it filled with 0, and filled ...
  • 它将result设置为(无符号)值,将8位value置于结果的最低8位。 这样的原因是必需的,即byte是Java中的一种签名类型。 如果你刚刚写道: int result = value; 那么result将以ff ff ff fe而不是00 00 00 fe 。 另一个微妙之处在于&被定义为只对int值1进行操作,所以发生的是: value被提升为int ( ff ff ff fe )。 0xff是一个int字面值( 00 00 00 ff )。 应用&来产生所需的result值。 (关键是转换为in ...
  • 问题不是很清楚 - 但是,处理字节最常见的问题是Java没有无符号字节,布尔运算总是在int之间 处理字节的最佳方法是使用整数,并使用0xff进行每个操作。 也可以使用>>>作为右移(它是未签名的版本) 例: byte b =(byte)(255&0xff)//给你“无符号字节” 字节b =(字节)((b << 2)0xff)//向左移位必须截断 如果您发布代码来计算CRC,我可以查看它 不使用负数定义字节数组的最佳方法是这样的: byte[]={ (byte)0xff, (byte)0xff, (byte ...
  • 如前所述,Java总是用int或更大的计算,所以 考虑b = 1111 0001 (注意顶部位设置,所以b实际上是-15) 隐式转换为int: (int)b = 1111 .... 1111 0001 做正确的转变: (b>>4) = 1111 1111 .... 1111 1111 (b>>>4) = 0000 1111 .... 1111 1111 显式转换为bool: c = d = 1111 1111 。 考虑e=(byte)((b&0xff)>>4); 隐式转换为int: (int)b = 111 ...
  • 就在这里。 使用带有\x作为前缀的单引号字符。 这表示十六进制文字char类型。 例如: '\xff' 。 但请注意, char可以是有符号或无符号的,包括C ++ 11,它甚至可以是1的补码签名类型。 Yes there is. Use single quotation characters with \x as the prefix. That denotes a hexadecimal literal char type. For example: '\xff'. But note that char ...
  • & 0xFF是多余的,在给定的情况下没有区别 & 0xFF is redundant and makes no difference in the given case
  • 正如@fuz建议的那样,这只是一个优化器的错误,它没有将foo & 0xff识别为在最初可能在原始函数中使用的上下文中的无操作。 在将项目的编译设置设置为“Release”后,我使用Borland C ++ Builder 6编译了以下代码片段: unsigned char foobar(int foo) { return (foo >> 16) & 0xff; } 这类似于您提供的非常密切的拆卸操作。 我们有一个32位的值,我们希望移位给定的位数,然后将其转换为字节值,基本上将原始值的16-23位作为一 ...
  • 肯定有更快的方法来优化您正在执行的特定检查。 一些评论已经表明的真正问题是它是否真的有必要? 是否值得优化查询真的取决于几个问题,你首先要问自己。 你的表现标准是什么? 您应该每秒处理多少个阵列? 如果答案是1000或更少,那么我肯定不会打扰尝试优化代码。 如果答案是每秒数百万个数组,那么您可能需要考虑对代码进行一些性能测试。 您期望什么类型的数据? 如果您处理的99%的缓冲区有效(并非所有0xFF字节),那么在大多数情况下,您的循环很可能在前几次检查中存在。 如果仅适用于1%的工作负载,那么在最坏情况下优 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。