首页
\
问答
\
为什么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)); }
相关问答
更多-
AND 0xFF是做什么的?(What does AND 0xFF do?)[2022-09-04]
与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 ...
-
如何将已签名的char初始化为C ++中的0xFF等无符号值?(How to initialize a signed char to unsigned values like 0xFF in C++?)[2022-03-06]
就在这里。 使用带有\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 ... -
将int转换为字节数组(Converting int to a byte array)[2022-05-08]
& 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%的工作负载,那么在最坏情况下优 ...