Java中具有3个线程的多线程
我正在尝试创建一个多线程程序来计算Pi。我的简单程序是:Java中具有3个线程的多线程,java,multithreading,Java,Multithreading,我正在尝试创建一个多线程程序来计算Pi。我的简单程序是: import java.util.Random; public class MonteCarloPi { static Random generator = new Random( System.currentTimeMillis() ); /** * @param args */ public static void main(String[] args) {
import java.util.Random;
public class MonteCarloPi {
static Random generator = new Random( System.currentTimeMillis() );
/**
* @param args
*/
public static void main(String[] args) {
long N = 10000000;
if ( args.length > 1 )
N = Long.parseLong( args[0] );
double PI = myPiCompute(N);
System.out.println( N + " random samples yield an approximation of Pi = " + PI);
}
public static double myPiCompute (long N){
long Nc = 0;
for ( long i=0; i<N; i++ ) {
float x = generator.nextFloat()*2 - 1; // random float in [-1,1]
float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
if ( x*x + y*y <= 1.0f )
Nc += 1;
}
double PI = 4.0*Nc/N;
return PI;
}
}
import java.util.Random;
蒙特卡洛比公务舱{
静态随机生成器=新随机(System.currentTimeMillis());
/**
*@param args
*/
公共静态void main(字符串[]args){
长N=10000000;
如果(参数长度>1)
N=Long.parseLong(args[0]);
双π=myPiCompute(N);
System.out.println(N+”随机样本产生Pi=“+Pi”的近似值);
}
公共静态双myPiCompute(长N){
长Nc=0;
对于(long i=0;i您的“myPiCompute”函数不在类中。在抛出错误的行上方有一个额外的右括号
在这里:
MyThread类实现可运行{
公开募捐{
System.out.println(“此线程正在运行…”);
长N=10000000;
如果(参数长度>1)
N=Long.parseLong(args[0]);
双π=myPiCompute(N);
System.out.println(N+”随机样本产生Pi=“+Pi”的近似值);
}
}//“我遇到了很多错误。”
--为什么不给我们看一下错误?以及导致错误的行数?这似乎是合乎逻辑的做法,不是吗?首先,数一数你的花括号,确保所有的开始大括号都与结束大括号匹配。在抛出错误的行数上方有一个额外的结束大括号。投票以打字错误结束。同意@SotiriosDelimanolis——这只是一个小括号错误。是的,因为每个人都忙于批评它而不是帮助我,我已经找到了答案并修复了它。我将很快为我的问题发布一个答案。不是你的否决选民,但这个问题需要关闭,因为它太具体,只是草率的编码,对未来的任何人都没有帮助。但是请随意否决我的公社请再次回答,以便更轻松地结束此问题。您最好使用IDE来帮助您验证语法。;-)请不要投票给这个答案,因为它可能会使问题更难删除。修复后,我会遇到更多错误。即使我在MyThread
@user2481422:start中的@param
中提供它,它也无法识别args
over@user2481422:这意味着您在创建此代码时粗心大意,您可能会如果你把事情弄得一团糟,连头绪都弄不清楚了,那就承认失败,删除代码,重新开始。这次要多加小心。我一定是在上学的时候做过一百次,最后只是把你弄得一团糟away节省了大量时间,因为您可以继续编写掌握的代码,而不是盲目地盯着看起来像外语的现有代码。
import java.util.Random;
class MyThread implements Runnable {
/**
* @param args
*/
static Random generator = new Random( System.currentTimeMillis() );
public void run() {
System.out.println("this thread is running ... ");
long N = 10000000;
if ( args.length > 1 )
N = Long.parseLong( args[0] );
double PI = myPiCompute(N);
System.out.println( N + " random samples yield an approximation of Pi = " + PI);
}
}
public static double myPiCompute (long N){
long Nc = 0;
for ( long i=0; i<N; i++ ) {
float x = generator.nextFloat()*2 - 1; // random float in [-1,1]
float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
if ( x*x + y*y <= 1.0f )
Nc += 1;
}
double PI = 4.0*Nc/N;
return PI;
}
public class MonteCarloPi {
public static void main(String[] args) {
MyThread myObject = new MyThread();
Thread thr1 = new Thread(myObject);
thr1.start();
Thread thr2 = new Thread(myObject);
thr2.start();
Thread thr3 = new Thread(myObject);
thr3.start();
}
}
MonteCarloPiPar.java:19: error: class, interface, or enum expected
public static double myPiCompute (long N){
^
MonteCarloPiPar.java:21: error: class, interface, or enum expected
for ( long i=0; i<N; i++ ) {
^
MonteCarloPiPar.java:21: error: class, interface, or enum expected
for ( long i=0; i<N; i++ ) {
^
MonteCarloPiPar.java:21: error: class, interface, or enum expected
for ( long i=0; i<N; i++ ) {
^
MonteCarloPiPar.java:23: error: class, interface, or enum expected
float y = generator.nextFloat()*2 - 1; // random float in [-1,1]
^
MonteCarloPiPar.java:24: error: class, interface, or enum expected
if ( x*x + y*y <= 1.0f )
^
MonteCarloPiPar.java:26: error: class, interface, or enum expected
}
^
MonteCarloPiPar.java:29: error: class, interface, or enum expected
return PI;
^
MonteCarloPiPar.java:30: error: class, interface, or enum expected
}
^
9 errors
class MyThread implements Runnable {
public void run() {
System.out.println("this thread is running ... ");
long N = 10000000;
if ( args.length > 1 )
N = Long.parseLong( args[0] );
double PI = myPiCompute(N);
System.out.println( N + " random samples yield an approximation of Pi = " + PI);
}
} // <<< *********