多线程-为所有变量请求一次输入(Java)
下面是多线程函数的代码和输出,其中有一个计数器和加法、减法、乘法和除法函数。我正在使用Eclipse 每个数学函数有4个线程:多线程-为所有变量请求一次输入(Java),java,multithreading,Java,Multithreading,下面是多线程函数的代码和输出,其中有一个计数器和加法、减法、乘法和除法函数。我正在使用Eclipse 每个数学函数有4个线程: public class Maths { public static void main(String args[]){ CalculationThread T1 = new CalculationThread("Addition"); T1.start(); CalculationThread T2 = n
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.");
}
}
}
上面的代码可以同时完成所有4个函数,但是每当我尝试包含一个JOptionPane以供用户输入而不是一个自动计数器时,4个线程中的每一个都会一次请求。因此,如果函数正在等待我输入2个数字,则它不算作多线程。如何以及以何种方式包含一个只需要用户在开始时输入的用户输入,以便所有函数都可以使用这两个变量。我不确定是否正确理解它 如果您只想阻塞计算线程并等待初始用户输入,则可以使用
信号灯
等待用户输入的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();
}
}
您可以参考以下代码。 根据我对你问题的理解,也许它会适合你的需要。我还根据装满鳗鱼的气垫船的建议,对代码应用了
.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.");
}
}
}
}
import javax.swing.JOptionPane;
公共课数学{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
字符串num1=JOptionPane.showInputDialog(“num1:”);
字符串num2=JOptionPane.showInputDialog(“num2:”);
int num11=整数.parseInt(num1);
int num22=整数.parseInt(num2);
calculationThread T1=新的calculationThread(“加法”);
T1.getNumber(num11,num22);
T1.start();
calculationThread T2=新的calculationThread(“减法”);
T2.getNumber(num11,num22);
T2.start();
calculationThread T3=新的calculationThread(“乘法”);
T3.获取编号(num11,num22);
T3.start();
calculationThread T4=新的calculationThread(“除法”);
T4.获取编号(num11,num22);
T4.开始();
}
}
类calculationThread扩展线程{
私有线程t;
私人字符串数学;
私人INTA;
私人INTB;
私有int resultplus=0;
私有int resultminus=0;
私有int resulttimes=0;
私有int resultdivide=0;
公共无效获取编号(整数num1,整数num2){
a=num1;
b=num2;
}
计算线程(字符串应答){
数学=答案;
}
公开作废开始(){
System.out.println(“开始计算“+数学+”\n”);
如果(t==null){
t=新线程(这是数学);
t、 start();
}
}
公开募捐{
试一试{
对于(int x=0;xSide问题,关于:if(math==“Addition”){
:不要使用=
或!=
比较字符串。使用等于(…)
或相等信号格(…)
方法。请理解==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这在这里很重要。另一个次要问题:如中所述,您应该编写一个在extendsThread
,重写run()
方法,创建类的实例,并调用start()
,或者编写实现Runnable
的类,创建类的实例,将实例传递给Thread
的构造函数,然后调用start()线程上的
实例。
class Calculation implements Runnable {
...
@override
public void run() {
// the name you passed to the thread is your math
// lets get it from the currently running thread where it is stored.
final String math = Thread.currentThread().getName();
...
}
}
// somewhere else
new Thread(new CalculationThread, math).start();
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.");
}
}
}
}