小编use*_*349的帖子

chrono steady_clock没有给出正确的结果?

我的app服务器代码中有一行代码,它使用steady_clock如下所示的时间戳值:

uint64_t now = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
Run Code Online (Sandbox Code Playgroud)

现在我们有两个正在运行的Ubuntu 12 (gcc 4.6.3 compiler)systemA 机器和正在运行的machineB Ubuntu 14 (gcc 4.8.2 compiler).

现在我们使用另一个 make编译我们的app服务器代码Ubuntu 12 VM (which has 4.7.3 compiler),然后将生成的tar文件复制到machineA并启动我们的app服务器.在开始之后,上面的代码行在machineA中打印出如下值:

1439944652967
Run Code Online (Sandbox Code Playgroud)

现在我们还使用另一个 make编译相同的应用服务器代码Ubuntu 14 VM (which has 4.8.2 compiler),然后将生成的tar文件复制到machineB并启动我们的app服务器.在开始之后,上面的代码行在machineB中打印出这样的值:

10011360 
Run Code Online (Sandbox Code Playgroud)

你看到差异吧?我很困惑为什么这是差异,我无法理解这一点?所有代码和一切都是一样的.有没有人对此有任何解释,我该如何解决?

如果需要,我可以尝试添加一些调试代码,看看弄清楚这个问题有什么不对.

c++ ubuntu gcc c++-chrono

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

如何使用maven制作可执行jar?

我的maven项目是这样的,我quartz.properties/src/main/resources文件夹中有一个文件,如下所示

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |           `-- app
    |   |               `-- App.java
    |   `-- resources
    |       `-- quartz.properties
    `-- test
        |-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java
Run Code Online (Sandbox Code Playgroud)

现在我想使用maven创建一个可执行jar,以便我可以像这样运行它java -jar abc.jar.下面是我的主要方法代码,在我的eclipse IDE中我的笔记本电脑工作正常,但我想在我的ubuntu机器上使用java -jar命令运行它:

public static void main(String[] args) {
    StdSchedulerFactory factory = new StdSchedulerFactory();
    try {
        factory.initialize(App.class.getClassLoader().getResourceAsStream("quartz.properties"));
        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
    } catch (SchedulerException …
Run Code Online (Sandbox Code Playgroud)

java jar classloader maven

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

如何有效地为List的所有元素添加前缀?

我有一个List,我需要在列表的所有元素中添加前缀.

下面是我通过迭代列表然后添加它的方式.还有其他更好的方法吗?任何一两个衬垫可以做同样的东西?

private static final List<DataType> DATA_TYPE = getTypes();

public static LinkedList<String> getData(TypeFlow flow) {
    LinkedList<String> paths = new LinkedList<String>();
    for (DataType current : DATA_TYPE) {
        paths.add(flow.value() + current.value());
    }
    return paths;
}
Run Code Online (Sandbox Code Playgroud)

我需要返回LinkedList,因为我使用的是LinkedList类的一些方法removeFirst.

我现在在Java 7上.

java collections list set

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

我怎样才能得到位的位置

我有一个十进制数,我需要转换为二进制,然后在二进制表示中找到一个的位置.

输入为5,二进制为101,输出应为

1
3
Run Code Online (Sandbox Code Playgroud)

下面是我的代码,它只提供输出,2而不是我想提供一个二进制表示的位置.如何从1开始获取设置位的位置?

public static void main(String args[]) throws Exception {
    System.out.println(countBits(5));
}

private static int countBits(int number) {
    boolean flag = false;

    if (number < 0) {
        flag = true;
        number = ~number;
    }
    int result = 0;
    while (number != 0) {
        result += number & 1;
        number = number >> 1;
    }
    return flag ? (32 - result) : result;
}
Run Code Online (Sandbox Code Playgroud)

java algorithm binary bit-manipulation

5
推荐指数
2
解决办法
8073
查看次数

使用GSON将POJO序列化为不同名称的JSON?

我有一个这样的POJO,我使用GSON序列化为JSON:

public class ClientStats {

    private String clientId;
    private String clientName;
    private String clientDescription;

    // some more fields here


    // getters and setters

}
Run Code Online (Sandbox Code Playgroud)

我是这样做的:

ClientStats myPojo = new ClientStats();

Gson gson = new Gson();
gson.toJson(myPojo);
Run Code Online (Sandbox Code Playgroud)

现在我的json将是这样的:

{"clientId":"100", ...... }
Run Code Online (Sandbox Code Playgroud)

现在我的问题是:有什么方法可以提出我自己的名字,clientId而不是更改clientId变量名称?在Gson中是否有任何注释我可以在clientId变量顶部使用?

我想要这样的东西:

{"client_id":"100", ...... }
Run Code Online (Sandbox Code Playgroud)

java json gson

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

无法从java程序执行R脚本?

我在String变量中有一个Rscript,我想从Java程序执行它并将一些变量传递给它.如果我独立执行该R脚本,它可以正常工作.我通过使用Python程序将所有R脚本转换为一行,如下所示:

import json

jsonstr = json.dumps({"script": """\
#!/usr/bin/Rscript

# read the data file
library('jsonlite')
library('rpart')

args <- as.list(Sys.getenv(c(
                        "path",
                        "client_users")))

if (args[["path"]]==""){
    args[["path"]] <- "."
}

# other stuff here
# other stuff here

"""})

print jsonstr
Run Code Online (Sandbox Code Playgroud)

我使用打印出来的字符串并将其存储在String变量中,然后我执行下面的代码,它根本不起作用.我传递pathclient_users变量到上面的R脚本.

public static void main(String[] args) throws IOException, InterruptedException {

    // this is your script in a string
    // String script = "#!/bin/bash\n\necho \"Hello World\"\n\n readonly PARAM1=$param1\n echo $PARAM1\n\nreadonly PARAM2=$param2\n echo $PARAM2\n\n";
    String script = "above R Script here"; …
Run Code Online (Sandbox Code Playgroud)

java bash runtime r processbuilder

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

如何更改挂载点名称?

我在ubuntu工作,这是df -h显示的:

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda         30G  1.7G   27G   6% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev             15G   12K   15G   1% /dev
tmpfs           3.0G  372K  3.0G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none             15G     0   15G   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/vdb        197G   60M  187G   1% /mnt
Run Code Online (Sandbox Code Playgroud)

现在我想改变的名称/mnt/data目录来代替.我希望所有内容都保持原样,唯一需要改变的是驱动器挂载点的名称.

这是我在/etc/fstab档案中的内容.

LABEL=c3image-rootfs    /               ext4    errors=remount-ro 0       1
/dev/vdb        /mnt    auto    defaults,nobootwait,comment=cloudconfig 0       2
Run Code Online (Sandbox Code Playgroud)

你能解释一下必要的命令和要编辑的文件吗?

linux directory mount

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

每次向另一个方法发送大约固定大小的字节数组

我有一个方法,它采用Partition枚举参数.通过传递不同的值,该方法将在同一时间段内由多个后台线程(最多15个)调用partition.这里dataHoldersByPartition是一个ImmutableMapPartitionConcurrentLinkedQueue<DataHolder>.

  private final ImmutableMap<Partition, ConcurrentLinkedQueue<DataHolder>> dataHoldersByPartition;

  //... some code to populate entry in `dataHoldersByPartition` map

  private void validateAndSend(final Partition partition) {  
    ConcurrentLinkedQueue<DataHolder> dataHolders = dataHoldersByPartition.get(partition);
    Map<byte[], byte[]> clientKeyBytesAndProcessBytesHolder = new HashMap<>();
    int totalSize = 0;      
    DataHolder dataHolder;
    while ((dataHolder = dataHolders.poll())  != null) {      
      byte[] clientKeyBytes = dataHolder.getClientKey().getBytes(StandardCharsets.UTF_8);
      if (clientKeyBytes.length > 255)
        continue;

      byte[] processBytes = dataHolder.getProcessBytes();
      int clientKeyLength = clientKeyBytes.length;
      int processBytesLength = processBytes.length;

      int additionalLength = …
Run Code Online (Sandbox Code Playgroud)

java arrays multithreading byte bytebuffer

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

来自单线程执行器的 RejectedExecutionException

下面是我的方法,其中我有单线程执行器来执行 run 方法中的某些任务。

  private void trigger(final Packet packet) {

    // this line is throwing exception
    Executors.newSingleThreadExecutor().execute(new Runnable() {
      @Override
      public void run() {
        // some code here
      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

以下是我遇到的异常,我不确定为什么?解决这个问题的最佳方法是什么?

error= java.util.concurrent.RejectedExecutionException: Task com.abc.stuffProc$2@e033da0 rejected from java.util.concurrent.ThreadPoolExecutor@76c2da8f[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2048)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:821)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1372)
    at java.util.concurrent.Executors$DelegatedExecutorService.execute(Executors.java:628)
Run Code Online (Sandbox Code Playgroud)

如果我的trigger方法被调用多次并且它仍在处理我之前线程中的 run 方法,会发生什么情况?它会启动尽可能多的线程还是会等待一个线程完成然后启动另一个线程?

java concurrency multithreading executorservice runnable

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

仅在匹配阈值字节时才在地图中填充字符串值

我有一个tasks列表对象,我正在迭代并附加每个任务对象,StringBuilder然后是新行,如下所示.现在我将继续在同一个字符串生成器中追加任务对象,直到它达到60000字节的大小限制.一旦达到限制,我将填充此字符串作为映射中的值,键将是带增量索引的文件名.然后我将重置字符串构建器和其他东西并再次重复此过程.

因此,如果我有一个大tasks对象,那么我将拆分成多个字符串对象,其大小应始终小于60000字节.

我得到了下面的代码,但我总是看到地图中的值大小超过60000字节.我做错了什么?此外,我正在填写HashMap两个不同的地方 - 一个达到限制,另一个是如果没有达到限制.

  public void populate(final List<Task> tasks) {
    Map<String, String> holder = new HashMap<>();
    int size = 0;
    int index = 0;
    StringBuilder sb = new StringBuilder();
    for (Task task : tasks) {
      sb.append(task).append(System.getProperty("line.separator"));
      size = sb.toString().getBytes(StandardCharsets.UTF_8).length;
      if (size > 60000) {
        String fileName = "tasks_info_" + index + ".txt";
        holder.put(fileName, sb.toString());
        index++;
        sb = new StringBuilder();
        size = 0;
      }
    }
    // for cases where we don't reach …
Run Code Online (Sandbox Code Playgroud)

java algorithm stringbuilder utf-8 data-structures

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