小编Mik*_*rov的帖子

是unmodifiableList线程安全吗?

我有一个字符串(tagList)列表需要在多个线程之间共享进行读取,所以我创建了一个不可修改的版本并将其传递给线程,我不确定它是否是线程安全的,因为线程只读取该列表所以我猜应该没问题?

当我将该不可修改的列表传递给线程时,它是否传递单个副本并由线程共享,还是创建多个副本并将一个副本传递给每个线程?

这是我的代码:

final List<String> tList = Collections.unmodifiableList(tagList);
List<Future<Void>> calls = new ArrayList<Future<Void>>();

FileStatus[] fsta = _fileSystem.listStatus(p);      
for (FileStatus sta : fsta) {
    final Path path = new Path(sta.getPath(), "data.txt");
    if (!_fileSystem.exists(path)) {        
        continue;
    }
    else {
        calls.add(_exec.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                filterData(path, tList);
                return null;
            }
        }));
    }
}
Run Code Online (Sandbox Code Playgroud)

java unmodifiable

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

如何在给定字符串中的四个空格后获取子字符串?

我有类似的字符串"align is going to school sad may me".我想在四个空格后得到子字符串.字符串将在运行时输入.任何人都可以建议我在一些空格后找到Sub String ......

String st = "align is going to school sad may me";

int i = 0;
String [] strings = new String [15];
StringTokenizer stringTokenizer = new StringTokenizer (st, " ");

while (stringTokenizer.hasMoreElements ())
{
    strings [i]= (String)stringTokenizer.nextElement ();
    i++;
}
System.out.println ("I value is" + i);

for (int j=4; j<i; j++)
{
    System.out.print (strings[j] + " ");
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了这个,它的工作可以请你建议我在一些空格后找到Sub字符串的简单方法.

java

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

Math.random打印出10个随机整数,但它们都是相同的数字?

在下面的程序中,使用时询问他想从哪个数字范围生成10个随机数,但是,一旦用户输入他的选择,打印的数字都是相同的?

import java.io.*;

public class RandomInt
{
    public static void main (String [] args) throws IOException
    {
        String inGuess;
        int theGuessInt = 0;
        int theNum;
        int theNum2;
        BufferedReader myInput = new BufferedReader (new InputStreamReader (
                System.in));
        System.out
                .println ("Hello this program generates random numbers from 0-10 or 20-30");
        System.out
                .println ("Please input '1' for the random generation of 0-10, or '2' for the generation of 20-30");
        inGuess = myInput.readLine ();
        theGuessInt = Integer.parseInt (inGuess);
        // Generate random numbers from 0 …
Run Code Online (Sandbox Code Playgroud)

java

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

标签 统计

java ×3

unmodifiable ×1