小编bja*_*mdd的帖子

C#.net 到 Java - 使用带密码的 AES 进行加密和解密

我试图在 Java 中重现以下加密/解密算法,但我找不到 Rfc2898DeriveBytes() 和 RijndaelManaged() 等多种方法的替代方法。我该怎么做呢?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace CryptingTest
{   
    public static class StringCipher
    {
        // This constant is used to determine the keysize of the encryption algorithm in bits.
        // We divide this by 8 within the code below to get the equivalent number of bytes.
        private const int Keysize = 128;

        // This constant determines the number of iterations for the password bytes generation function.
        private …
Run Code Online (Sandbox Code Playgroud)

java encryption aes

6
推荐指数
1
解决办法
3125
查看次数

使用 Task.Run() 时的延迟

我正在通过 ZMQ 读取数据并根据其主题在单独的线程中执行任务。我注意到,当数据频率非常高(每 1 毫秒约 1 条消息)时,某些任务需要很长时间才能执行。

这基本上就是我正在做的事情:

while(true){
  item = zmqSubscriber.ReceiveData(out topic, out ConsumeErrorMsg);
  if (topic.Equals(topic1))
     {
      Task.Run(() => ExecuteTask1(item));
     }
     else if (topic.Equals(topic2)
     {
      Task.Run(() => ExecuteTask2(item));
     }
     else if (topic.Equals(topic3))
     {
      Task.Run(() => ExecuteTask3(item));

     }
}
Run Code Online (Sandbox Code Playgroud)

当数据频率较低时(每 100 毫秒 10 条消息),我没有注意到任何行为问题。

我是 C# 新手,我想知道这是否可能是由于活动线程池线程的最大数量较低所致。我在这里读到数量可以增加,但是,这不是一个好的做法:ThreadPool.SetMinThreads(Int32, Int32) Method

所以我想知道是否有更好的方法来实现我想要做的事情?

c# multithreading zeromq

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

如何在 ConcurrentQueue 出队时设置最大并发线程数?

我有一个线程负责入队,一个线程负责出队。然而,数据入队的频率远远超过出队+处理数据所需的时间。当我执行以下操作时,数据处理出现了巨大的延迟:

public void HandleData()
{
    while (true)
    {
        try
        {
            if (Queue.Count > 0)
            {
                Queue.TryDequeue(out item);
                ProcessData(item);
            }
            else
            {
                Thread.Sleep(10);
            }
        }
        catch (Exception e)
        {
            //...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试在单独的任务中处理数据,但这最终会影响项目中的其他任务,因为这种处理最终会占用分配给应用程序的大部分资源并生成大量线程。

public void HandleData()
{
    while (true)
    {
        try
        {
            if (Queue.Count > 0)
            {
                Queue.TryDequeue(out item);
                Task.Run(() => ProcessData(item));
            }
            else
            {
                Thread.Sleep(10);
            }
        }
        catch (Exception e)
        {
            //
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来,我尝试了以下操作:

public void HandleData()
{
    List<Task> taskList = new List<Task>();
    while (true)
    {
        try …
Run Code Online (Sandbox Code Playgroud)

c# multithreading throttling task-parallel-library concurrent-queue

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