好的,现在我得到了我的输出但是,一切似乎都不合适。我试了好几天。现在我更多地了解多线程。如何获得我预期的输出?有人可以告诉我哪个部分做错了吗?
这是我的测试程序
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)
使用wait并通过synchronized
通知等待使线程等待并通知唤醒其他等待线程。
请填写条件等待状态和通知条件相同。
synchronized public void buy(int shares, double pricePerShare)
{
if(totalShares>0)
{
wait();
}
totalShares += shares;
totalCost += shares * pricePerShare;
System.out.println ("Total shares now at " +totalShares+ " at total cost $ " +totalCost);
notify();
}
synchronized public void sell(int shares, double pricePerShare)
{
double sellCost = shares * pricePerShare;
if (shares >= totalShares || sellCost >= totalCost)
{
wait();
}
else
{
totalShares -= shares;
totalCost -= sellCost;
System.out.println ("Total shares now at " +totalShares+ " at total cost $ "+totalCost);
notify();
}
}