在 java 中使用多个线程添加数字
Adding numbers using multiple threads in java
我无法弄清楚我的代码在做什么,因为这是我第一次使用多线程进行编码。首先,为了学习这种类型的编程,我决定编写一个使用 8 个线程对数字求和的微型程序。然而,无论我做什么,当计数 = 10 时,我的程序似乎永远不会停止,它会继续前进。我正在使用 8 个线程,因为我计划扩展我的程序来进行大型计算。但是,这些线程根本不相关。他们已经过了 10 点了。我使用了同步方法。我试过锁。我尝试同时实施两者。无论如何,线程似乎仍在计算超过 10。请参阅下面的当前代码。
public class calculator implements Runnable {
static int counter = 0;
static int sum = 0;
private synchronized static int getAndIncrement()
{
// System.out.println("counter is : " + counter);
int temp = counter;
counter = counter + 1;
System.out.println("counter is now : " + counter);
return temp;
}
private synchronized void addToSum(int value)
{
// System.out.println("sum : " + sum + " value: " + value);
sum += value;
}
@Override
public void run()
{
// TODO Auto-generated method stub
while(counter < 10)
{
int tempVal = getAndIncrement();
System.out.println("temp val : " + tempVal);
addToSum(tempVal);
// System.out.println("sum is now : " + sum);
}
}
}
这是我的主要方法:
public static void main(String[] args)
{
calculator[] calc = new calculator[8];
Thread[] thread = new Thread[8];
final long startTime = System.currentTimeMillis();
for(int i = 0; i < 8; i++)
{
calc[i] = new calculator();
thread[i] = new Thread(calc[i]);
thread[i].start();
}
while(thread[0].isAlive() ||thread[1].isAlive() || thread[2].isAlive() || thread[3].isAlive() || thread[4].isAlive() || thread[5].isAlive() || thread[6].isAlive() || thread[7].isAlive())
{}
final long endTime = System.currentTimeMillis();
System.out.println(calculator.sum);
System.out.println("Execution time : " + (startTime - endTime));
}
非常感谢您的帮助!
synchronized
关键字取对象
锁。这意味着 synchronized
的两个方法不能在同一个对象 上执行 。但是,它们将在调用 2 个不同对象时并发执行。
在您的示例中,您的代码有 8 个 calculator
对象。 synchronized
方法对您没有帮助。每个线程都使用它的单独对象。您可以完全删除 synchronized
关键字,您的代码在语义上将是等效的。
为避免这种情况,请使用对象的原子版本 (AtomicInt
) 或锁定对象本身:synchronized(counter){...}
但要使其正常工作,您必须将类型更改为 Integer
.
我无法弄清楚我的代码在做什么,因为这是我第一次使用多线程进行编码。首先,为了学习这种类型的编程,我决定编写一个使用 8 个线程对数字求和的微型程序。然而,无论我做什么,当计数 = 10 时,我的程序似乎永远不会停止,它会继续前进。我正在使用 8 个线程,因为我计划扩展我的程序来进行大型计算。但是,这些线程根本不相关。他们已经过了 10 点了。我使用了同步方法。我试过锁。我尝试同时实施两者。无论如何,线程似乎仍在计算超过 10。请参阅下面的当前代码。
public class calculator implements Runnable {
static int counter = 0;
static int sum = 0;
private synchronized static int getAndIncrement()
{
// System.out.println("counter is : " + counter);
int temp = counter;
counter = counter + 1;
System.out.println("counter is now : " + counter);
return temp;
}
private synchronized void addToSum(int value)
{
// System.out.println("sum : " + sum + " value: " + value);
sum += value;
}
@Override
public void run()
{
// TODO Auto-generated method stub
while(counter < 10)
{
int tempVal = getAndIncrement();
System.out.println("temp val : " + tempVal);
addToSum(tempVal);
// System.out.println("sum is now : " + sum);
}
}
}
这是我的主要方法:
public static void main(String[] args)
{
calculator[] calc = new calculator[8];
Thread[] thread = new Thread[8];
final long startTime = System.currentTimeMillis();
for(int i = 0; i < 8; i++)
{
calc[i] = new calculator();
thread[i] = new Thread(calc[i]);
thread[i].start();
}
while(thread[0].isAlive() ||thread[1].isAlive() || thread[2].isAlive() || thread[3].isAlive() || thread[4].isAlive() || thread[5].isAlive() || thread[6].isAlive() || thread[7].isAlive())
{}
final long endTime = System.currentTimeMillis();
System.out.println(calculator.sum);
System.out.println("Execution time : " + (startTime - endTime));
}
非常感谢您的帮助!
synchronized
关键字取对象
锁。这意味着 synchronized
的两个方法不能在同一个对象 上执行 。但是,它们将在调用 2 个不同对象时并发执行。
在您的示例中,您的代码有 8 个 calculator
对象。 synchronized
方法对您没有帮助。每个线程都使用它的单独对象。您可以完全删除 synchronized
关键字,您的代码在语义上将是等效的。
为避免这种情况,请使用对象的原子版本 (AtomicInt
) 或锁定对象本身:synchronized(counter){...}
但要使其正常工作,您必须将类型更改为 Integer
.