小编div*_*ine的帖子

实现Comparable的Java警告

我正在尝试在自定义对象的ArrayList上使用Collections.sort,但我收到警告,我无法弄清楚为什么

Warning: Type safety: Unchecked invocation 
sort(ArrayList<CharProfile>) of the generic method sort(List<T>) 
of type Collections
Run Code Online (Sandbox Code Playgroud)

使用此代码:

ArrayList<CharProfile> charOccurrences = new ArrayList<CharProfile>();

...

Collections.sort(charOccurrences);
Run Code Online (Sandbox Code Playgroud)

这是我的方法:

public class CharProfile implements Comparable {

...

@Override
public int compareTo(Object o) {

        if (this.probability == ((CharProfile)o).getProbability()) {
            return 0;
        }
        else if (this.probability > ((CharProfile)o).getProbability()) {
            return 1;
        }
        else {
            return -1;
        }
 }
}
Run Code Online (Sandbox Code Playgroud)

java

8
推荐指数
1
解决办法
7775
查看次数

按位运算符的负操作数如何在Java中工作?

-4 & -5 = -8 // How?
-4 & 5 = 4 // How?
Run Code Online (Sandbox Code Playgroud)

我需要解释如何达到上述结果.我用正整数求解没有困难.

java bitwise-operators

7
推荐指数
1
解决办法
777
查看次数

在 nodejs 中创建 workerThread 时,它是否使用与运行 nodejs 进程相同的核心?

假设我有nodejs serverProgram一个 api,它对通过 http 请求发送的视频文件进行了一些操作。

const saveVideoFile=(req,res)=>{
  processAndSaveVideoFile(); // can run for minimum of 10 minutes
  res.send({status: "video is being processed"})
}
Run Code Online (Sandbox Code Playgroud)

我决定使用 aworkerThread来做这个处理,因为我的机器有 3 个内核(core1、core2、core3)并且这里没有启用超线程

假设我的 nodejs 程序在core1. 当我火了一个单一的WorkerThread,将在运行的WorkerThreadcore2/core3还是core1

我读到 workerThread 与 childProcess 不同。ChildProcess 将派生一个新进程,这将有助于 childProcess 从可用的免费内核(core2 或 core3)中进行选择。

我读到 workerThread 与 mainThread 共享内存。假设我创建了 2 个 workerThreads (wt1,wt2)。我的 nodejs 程序 wt1、wt2 会在同一个核心上运行core1吗?

此外,在 nodejs 中,我们有 eventloop(主线程)和 otherThreads 执行后台操作,即 I/O。假设所有这些都在利用单个内核 (core1) 中可用的资源是否正确。如果是这种情况,是否在 nodejs 服务器上创建和使用额外的 workerThread 是一种矫枉过正?

下面是这个博客 …

multithreading worker-thread node.js

2
推荐指数
1
解决办法
145
查看次数

与java中的缓冲读者混淆

  1. 当我只用// 1 - start运行代码时,输​​出是输入字符的ascii值
  2. 当我仅使用// 2 - start运行代码时,输​​出是由换行符终止的输入字符串
  3. 当我用两者运行代码时(如下面的代码所示),只有// 1 - 开始执行,readLine()正在以一种奇怪的方式被考虑.
  4. 当我用两者运行代码时(// 2 - 开始位于// 1 - start之上),两个代码都执行正常,

请解释为什么这种奇怪的行为发生在案例3中但不是案例4中.

public class InputBufferedReader {

  public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
        //1-start
        //read one character
        System.out.println("enter input: ");
        int b= br.read();
        System.out.println(b);
        //end

        //2-start
        //read a string
        System.out.println("enter input string: ");
        String a = br.readLine();
        System.out.println(a);
        //end
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
125
查看次数