我正在做多线程。我无法得到我想要的输出。我没有编译错误。但我无法得到我想要的结果。有人可以告诉我吗?
这是我的测试程序
import java.util.*;
public class Test {
public static void main(String[] args) {
int totalShares = 0;
double totalCost = 0.0;
double profit = 0.0;
int rand1 = 0;
double rand2 = 0.0;
Thread bt[] = new Thread[2];
Thread st[] = new Thread[2];
for (int i = 0; i < 2; i++) {
try {
bt[i].join();
bt[i] = new BuyThread(rand1, rand2);
bt[i].start();
} catch (InterruptedException e) {
}
}
for (int i = 0; i < 2; i++) {
try {
st[i].join();
st[i] = new SellThread(rand1, rand2);
st[i].start();
} catch (InterruptedException e) {
}
}
System.out.printf("Tracking of stock : FCS");
System.out.println("Total shares now" + totalShares + "at total cost" + totalCost);
System.out.printf("At$ 36.00 per share, profit is" + profit);
}
}
这是我的股票类
import java.util.*;
public class Stock {
String name;
int totalShares;
double totalCost;
double totalProfitLoss;
public Stock(String theName) {
name = theName;
totalShares = 0;
totalCost = 0.00;
totalProfitLoss = 0.00;
}
public String getName() {
return name;
}
public double getTotalShares() {
return totalShares;
}
public double getTotalCost() {
return totalCost;
}
public void buy(int shares, double pricePerShare) {
totalShares += shares;
totalCost += shares * pricePerShare;
}
public boolean sell(int shares, double pricePerShare) {
double sellCost = shares * pricePerShare;
if (shares <= totalShares && sellCost <= totalCost) {
totalShares -= shares;
totalCost -= sellCost;
return true;
} else {
return false;
}
}
public double profit() {
totalProfitLoss = 36 * totalShares;
return totalProfitLoss - totalCost;
}
}
enter code here
这是我的BuyThread计划
import java.util.*;
public class BuyThread extends Thread {
private int share;
private double cost;
private Stock stock;
public BuyThread(int share, double cost) {
this.share = share;
this.cost = cost;
this.stock = stock;
}
public void run() {
try {
for (int j = 0; j < 5; j++)
Thread.sleep(100);
Random randShare = new Random();
int rand1 = randShare.nextInt(50) + 5; // value between 5 to 50
double rand2 = (int) ((Math.random() * 10 + 32) * 100.0) / 100.0; // value between 32.00 to 42.00
stock.buy(rand1, rand2);
System.out.println(rand1 + " shares has been brought at $" + rand2);
} catch (InterruptedException e) {
}
}
}
这是我的SellThread
import java.util.*;
public class SellThread extends Thread {
private int share;
private double cost;
private Stock stock;
public SellThread(int share, double cost) {
this.share = share;
this.cost = cost;
this.stock = stock;
}
public void run() {
for (int j = 0; j < 2; j++) {
try {
Thread.sleep(60);
Random randShare = new Random();
int rand1 = randShare.nextInt(20) + 20;
double rand2 = (int) ((Math.random() * 32 + 23) * 100.0) / 100.0;
stock.sell(rand1, rand2);
System.out.println(rand1 + "shares has been brought at $" + rand2);
} catch (InterruptedException e) {
}
}
}
}
我期待这样的输出(我运行错误:
Tracking of Stock : FCS
8 shares has been brought at $37.61
Total shares now 8 at total cost $300.88
33 shares has been brought at $36.31
Total shares now 41 at total cost $1499.11
17 shares has been sold at $42.67
Total shares now 24 at total cost $773.72
19 shares has been sold at $32.31
Total shares now 5 at total cost $159.83
31 shares has been brought at $33.85
Total shares now 36 at total cost $1209.18
28 shares has been brought at $36.37
Total shares now 64 at total cost $2227.54
20 shares has been brought at $35.49
Total shares now 84 at total cost $2937.34
At $36.00 per share, profit is $86.66
好的,现在我得到了我的输出但是,一切似乎都不合适。我试了好几天。现在我更多地了解多线程。如何获得我预期的输出?有人可以告诉我哪个部分做错了吗?
这是我的测试程序
import java.util.*;
public class Test
{
public static void main (String[]args)
{
int totalShares = 0;
double totalCost = 0.0;
double totalProfitLoss = 0.0;
int rand1 = 0;
double rand2 = 0.0;
Stock stock = new Stock("FCS",totalShares,totalCost,totalProfitLoss);
System.out.println ("Tracking of stock: " +stock.name);
BuyThread bt = new BuyThread (rand1 , rand2 , stock);
SellThread st = new SellThread (rand1 , rand2 , stock);
bt.start();
st.start();
try
{
bt.join();
st.join();
System.out.println ("At$ 36.00 per share, profit is " + stock.getProfit(36.00));
}
catch (InterruptedException e)
{}
}
}
这是我的股票类
import java.util.*;
public class Stock
{
String name;
int totalShares;
double totalCost;
double totalProfitLoss;
public Stock(String name, int totalShares, double totalCost,double totalProfitLoss )
{
this.name = name;
this.totalShares = totalShares;
this.totalCost = totalCost;
this.totalProfitLoss = totalProfitLoss;
}
public String getName()
{
return name;
}
public double getTotalShares ()
{
//totalShares++;
return totalShares;
}
public double getTotalCost ()
{
// totalCost++;
return totalCost;
}
public double getTotalProfitLoss()
{
return totalProfitLoss;
}
public void buy(int shares, double pricePerShare)
{
totalShares += shares;
totalCost += shares * pricePerShare;
System.out.println ("Total shares now at " +totalShares+ " at total cost $ " +totalCost);
}
public boolean sell(int shares, double pricePerShare)
{
double sellCost = shares * pricePerShare;
if (shares >= totalShares || sellCost >= totalCost){
return false;
}
else {
totalShares -= shares;
totalCost -= sellCost;
System.out.println ("Total shares now at " +totalShares+ " at total cost $ "+totalCost);
return true;
}
}
public double getProfit (double currentPrice)
{
totalProfitLoss = currentPrice * totalShares;
return totalProfitLoss - totalCost;
}
}
这是我的BuyThread计划
import java.util.*;
public class BuyThread extends Thread
{
private int rand1 ;
private double rand2;
private Stock stock;
public BuyThread (int rand1,double rand2,Stock stock)
{
this.rand1 = rand1 ;
this.rand2 = rand2 ;
this.stock = stock;
}
public void run()
{
for (int j = 0 ; j < 2; j++)
{
Random randShare = new Random ();
int rand1 = randShare.nextInt (50) + 5 ;
double rand2 = (int) ((Math.random() * 10 + 32)* 100.0)/100.0 ;
stock.buy (rand1, rand2);
System.out.println (rand1+ " shares has been brought at $"+rand2) ;
try
{ sleep (50);
}
catch (InterruptedException e)
{}
}
}
}
这是我的SellThread
import java.util.*;
public class SellThread extends Thread
{
private int rand1 ;
private double rand2;
private Stock stock;
public SellThread (int rand1,double rand2,Stock stock)
{
this.rand1 = rand1 ;
this.rand2 = rand2 ;
this.stock = stock;
}
public void run()
{
for (int j = 0 ; j < 2; j++)
{
Random randShare = new Random ();
int rand1 = randShare.nextInt (20) + 20 ;
double rand2 = (int) ((Math.random() * 32 + 23)* 100.0)/100.0 ;
stock.sell (rand1 , rand2);
System.out.println (rand1+" shares has been sold at $"+rand2) ;
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{}
}
}
}
我得到了这个输出
Tracking of stock: FCS
Sell Total shares now at 13 at total cost $ 381.0999999999997
Buy Total shares now at 50 at total cost $ 1664.9999999999998
37 shares has been sold at $34.7
50 shares has been brought at $33.3
32 shares has been sold at $51.97
Buy Total shares now at 18 at total cost $ 555.1499999999996
5 shares has been brought at $34.81
At$ 36.00 per share, profit is 92.85000000000036
我期待这样的输出(我运行错误:
Tracking of Stock : FCS
8 shares has been brought at $37.61
Total shares now 8 at total cost $300.88
33 shares has been brought at $36.31
Total shares now 41 at total cost $1499.11
17 shares has been sold at $42.67
Total shares now 24 at total cost $773.72
19 shares has been sold at $32.31
Total shares now 5 at total cost $159.83
31 shares has been brought at $33.85
Total shares now 36 at total cost $1209.18
28 shares has been brought at $36.37
Total shares now 64 at total cost $2227.54
20 shares has been brought at $35.49
Total shares now 84 at total cost $2937.34
At $36.00 per share, profit is $86.66
答案 0 :(得分:0)
您传递的是rand1
和rand2
Random
类型。
您的BuyThread
构造函数定义为int
和double
。