下面是多线程函数的代码和输出,其中有一个计数器和加,减,乘和除的函数。我正在使用Eclipse。
每个数学函数的4个线程:
public class Maths {
public static void main(String args[]){
CalculationThread T1 = new CalculationThread("Addition");
T1.start();
CalculationThread T2 = new CalculationThread("Subtraction");
T2.start();
CalculationThread T3 = new CalculationThread("Multiplication");
T3.start();
CalculationThread T4 = new CalculationThread("Division");
T4.start();
}
}
class CalculationThread extends Thread{
private Thread t;
private String maths;
private int count = 0;
private int resultplus, resultminus, resulttimes, resultdivide = 0;
CalculationThread(String answer){
maths = answer;
}
public void start(){
System.out.println("Starting calculation of " + maths + "\n");
if(t == null){
t = new Thread (this, maths);
t.start();
}
}
这是函数发生的地方,它将使用计数器作为2个数字来执行等式。
public void run(){
try {
for (int x=0; x<=3 ; x++){
if(maths == "Addition"){
System.out.println("Calculating: " + maths + " of " + count +
" + "+ count + " = " + resultplus + "\n");
Thread.sleep(3000);
count++;
resultplus = count + count;
}
else if(maths == "Subtraction"){
System.out.println("Calculating: " + maths + " of " + count +
" - "+ count + " = " + resultminus + "\n");
Thread.sleep(3000);
count++;
resultminus = count - count;
}
else if(maths == "Multiplication"){
System.out.println("Calculating: " + maths + " of " + count +
" * "+ count + " = " + resulttimes + "\n");
Thread.sleep(3000);
count++;
resulttimes = count * count;
}
else if(maths == "Division"){
System.out.println("Calculating: " + maths + " of " + count +
" / "+ count + " = " + resultdivide + "\n");
Thread.sleep(3000);
count++;
resultdivide = count / count;
}
}
}
catch (InterruptedException e){
System.out.println("Math function failed");
}
if(maths == "Addition"){
System.out.println("Addition completed.");
}
else if(maths == "Subtraction"){
System.out.println("Subtraction completed.");
}
else if(maths == "Multiplication"){
System.out.println("Multiplication completed.");
}
else if(maths == "Division"){
System.out.println("Division completed.");
}
}
}
输出:
Starting calculation of Addition
Starting calculation of Subtraction
Calculating: Addition of 0 + 0 = 0
Starting calculation of Multiplication
Calculating: Subtraction of 0 - 0 = 0
Starting calculation of Division
Calculating: Multiplication of 0 * 0 = 0
Calculating: Division of 0 / 0 = 0
Calculating: Subtraction of 1 - 1 = 0
Calculating: Addition of 1 + 1 = 2
Calculating: Multiplication of 1 * 1 = 1
Calculating: Division of 1 / 1 = 1
Calculating: Addition of 2 + 2 = 4
Calculating: Subtraction of 2 - 2 = 0
Calculating: Division of 2 / 2 = 1
Calculating: Multiplication of 2 * 2 = 4
Calculating: Subtraction of 3 - 3 = 0
Calculating: Addition of 3 + 3 = 6
Calculating: Division of 3 / 3 = 1
Calculating: Multiplication of 3 * 3 = 9
Subtraction completed.
Addition completed.
Division completed.
Multiplication completed.
上面的代码可以同时完成所有4个函数,但每当我尝试为用户输入而不是自动计数器包含JOptionPane时,4个线程中的每一个都会一次请求。因此,如果函数等待我输入2个数字,则不计入多线程。如何以及以何种方式包含用户输入,只需要用户在开头输入,以便所有函数都可以使用2个变量。
答案 0 :(得分:1)
我不确定我是否理解正确。
如果您只想阻止计算线程并等待初始用户输入,您可以使用Semaphore
。
等待用户输入的UI线程显示对话框,并通过设置许可/线程数来释放等待计算线程。
这是一个如何看起来的例子(它也使用更面向对象的方法)。为简单起见,我跳过了乘法和除法任务
import java.util.concurrent.Semaphore;
import javax.swing.JOptionPane;
public class MathSample {
// because updated / read from different threads mark as volatile
private volatile int a, b;
// semaphore with no initial permits i.e.
// the calculations will wait until permits are available.
private Semaphore available = new Semaphore(0);
private abstract class Task implements Runnable {
public abstract void doCalculation();
public abstract String getName();
@Override
public void run() {
try {
// wait until a permit becomes available
available.acquire();
// not sure what should happen here
// wait again for user input?
for (int x = 0; x < 50; ++x) {
a = a + x;
doCalculation();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(String.format("Task %s completed.", getName()));
}
}
private class AdditionTask extends Task {
public void doCalculation() {
System.out.println(String.format("Calculating: Addition of + %d + %d = %d", a, b, a+b));
}
public String getName() {
return "Addition";
}
}
private class SubstractionTask extends Task {
public void doCalculation() {
System.out.println(String.format("Calculating: Substraction of + %d - %d = %d", a, b, a-b));
}
public String getName() {
return "Substraction";
}
}
private void run() {
new Thread(new AdditionTask()).start();
new Thread(new SubstractionTask()).start();
a = Integer.parseInt(JOptionPane.showInputDialog("First value"));
b = Integer.parseInt(JOptionPane.showInputDialog("Second value"));
available.release(2); // let the 2 calculation threads run
}
public static void main(String ...args) {
new MathSample().run();
}
}
正如您所看到的,您不必覆盖线程的start
方法来运行不同的线程。
您start
的{{1}}方法至少是奇怪的,因为您覆盖了CalculationThread
类的start方法,并在其中创建了另一个Thread实例,您传递了Thread
作为CalculationThread
。
更容易/更好:
Runnable
答案 1 :(得分:0)
您可以参考以下代码。
根据我对你问题的理解,也许它会满足你的需求。我还根据Hovercraft Full Of Eels建议将.equals()
应用于代码。
import javax.swing.JOptionPane;
public class Maths {
public static void main(String[] args) {
// TODO Auto-generated method stub
String num1 = JOptionPane.showInputDialog("Num1: ");
String num2 = JOptionPane.showInputDialog("Num2: ");
int num11 = Integer.parseInt(num1);
int num22 = Integer.parseInt(num2);
calculationThread T1 = new calculationThread("Addition");
T1.getNumber(num11, num22);
T1.start();
calculationThread T2 = new calculationThread("Subtraction");
T2.getNumber(num11, num22);
T2.start();
calculationThread T3 = new calculationThread("Multiplication");
T3.getNumber(num11, num22);
T3.start();
calculationThread T4 = new calculationThread("Division");
T4.getNumber(num11, num22);
T4.start();
}
}
class calculationThread extends Thread{
private Thread t;
private String maths;
private int a;
private int b;
private int resultplus = 0;
private int resultminus = 0;
private int resulttimes = 0;
private int resultdivide = 0;
public void getNumber(int num1, int num2){
a = num1;
b = num2;
}
calculationThread(String answer){
maths = answer;
}
public void start(){
System.out.println("Starting calculation of " + maths + "\n");
if(t == null){
t = new Thread(this, maths);
t.start();
}
}
public void run(){
try {
for (int x=0; x<=3 ; x++){
if(maths.equals("Addition")){
System.out.println("Calculating: " + maths + " of " + a +
" + "+ b + " = " + resultplus + "\n");
Thread.sleep(3000);
resultplus = a + b;
}
else if(maths.equals("Subtraction")){
System.out.println("Calculating: " + maths + " of " + a +
" - "+ b + " = " + resultminus + "\n");
Thread.sleep(3000);
resultminus = a - b;
}
else if(maths.equals("Multiplication")){
System.out.println("Calculating: " + maths + " of " + a +
" * "+ b + " = " + resulttimes + "\n");
Thread.sleep(3000);
resulttimes = a * b;
}
else if(maths.equals("Division")){
System.out.println("Calculating: " + maths + " of " + a +
" / "+ b + " = " + resultdivide + "\n");
Thread.sleep(3000);
resultdivide = a / b;
}
}
}
catch (InterruptedException e){
System.out.println("Math function failed");
}
finally{
if(maths.equals("Addition")){
System.out.println("Addition completed.");
}
else if(maths.equals("Subtraction")){
System.out.println("Subtraction completed.");
}
else if(maths.equals("Multiplication")){
System.out.println("Multiplication completed.");
}
else if(maths.equals("Division")){
System.out.println("Division completed.");
}
}
}
}