小编use*_*258的帖子

Java多线程概念和join()方法

我对join()Java中的Threads中使用的方法感到困惑.在以下代码中:

// Using join() to wait for threads to finish.
class NewThread implements Runnable {

    String name; // name of thread
    Thread t;

    NewThread(String threadname) {
        name = threadname;
        t = new Thread(this, name);
        System.out.println("New thread: " + t);
        t.start(); // Start the thread
    }
// This is the entry point for thread.

    public void run() {
        try {
            for (int i = 5; i > 0; i--) {
                System.out.println(name + ": " + i);
                Thread.sleep(1000);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized pthread-join

57
推荐指数
2
解决办法
11万
查看次数

安装MPI for Windows

我正在尝试为Windows 8安装MPI,所以当我搜索网络时,我得到了在XP/7上安装它的步骤,但不适用于Windows 8.链接是:http://swash.sourceforge.net/online_doc/swashimp/node9 html的

但首先,当我必须允许mpi.exe和smpd.exe通过防火墙进行通信时,这些exe文件未列出.其次,当我运行cmd(以管理员身份)并键入:"smpd -install"时,它会显示:"未知选项:-install".我猜Windows 8的命令是别的.如果有人帮助我,我将非常感激,因为我无法继续前进.

mpi windows-8.1

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

使用python简化有理数

我正在研究python中处理有理数的问题,它有一个简化它的方法.例如12/8给出3/2.我已经完成了这个问题,并得到了正确的答案,但我已经找到了分子和分母的gcd.可能有人帮助使用一些内置的特殊python功能或函数,模块或python独有的任何东西,就像你说的"Pythonic方式!"

是否有这样的方法或任何测试案例应该包括在内以涵盖所有可能性?

这是我的代码:

class RationalNumber:
def __init__(self, n, d=1):
    self.n=n
    self.d=d

'''def gcd(self, a, b): // I have taken out gcd by two methods: recursion and while loop
    if b>a:
        t=a
        a=b
        b=t

    while a%b != 0:
        r=a%b
        a=b
        b=r

    return b
    '''

def gcd(self, a, b):
    if a%b==0:
        return b
    else:
        return self.gcd(b, a%b)

def simplify(self):
    x=self.gcd(self.n, self.d)
    self.n=self.n/x
    self.d=self.d/x

    return RationalNumber(self.n, self.d)

def __str__(self):
    print "%s/%s"%(self.n, self.d)

r1 = RationalNumber(12,8)
print r1.simplify()
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,它会给出答案并给出错误:

Traceback …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 greatest-common-divisor

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

HashMap 中不区分大小写的搜索

我有一张地图:

static Map<String, String> = getMap(); //getting a map from a config file.
Run Code Online (Sandbox Code Playgroud)

现在,在这张地图中,我需要使用 KEYS 执行不区分大小写的搜索。我没有将值放入映射中,不是通过 put 函数,但您可以将其视为以键值格式存储在数据库中的值,并将其作为映射检索。我需要进行案例不敏感搜索。

经过研究,使用TreeMap可以解决问题,但效率不高 --> O(log n)

或者覆盖HashMap的get()方法,创建我自己的HashMap。但这将包括覆盖许多方法,而且我不想要这么多,它不是非常重要的代码的一部分。

现在我正在将这些值以小写形式存储在数据库中并进行检查。但这使得数据库中容易出错且不可读。

是否可以有更简单的方法来做到这一点?

java dictionary hashmap hashcode case-insensitive

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

使用用户定义的类数组并初始化它时出现Java NullPointerException错误

在下面的代码中,任何人都可以告诉错误吗?

public class Person {
    int age;
    String name;
}

public class Enter {

    public static void main(String[] args) {

        Enter ob = new Enter();
        Person p[] = new Person[5];

        p[0].name = "abc"; //Line 13
        p[0].age = 67;
            //I have initialized the whole array likewise.
    }
}
Run Code Online (Sandbox Code Playgroud)

但在第13行,它给出了以下错误:

Enter.main中线程"main"java.lang.NullPointerException中的异常(SorAccToAge.java:13)

java arrays nullpointerexception

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