Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java股票程序随机多线程_Java_Multithreading - Fatal编程技术网

Java股票程序随机多线程

Java股票程序随机多线程,java,multithreading,Java,Multithreading,我正在做多线程。我无法获得我想要的输出。我没有编译错误。但是我不能得到我想要的结果。有人能给我建议吗 这是我的测试程序 好的,现在我得到了我的输出,但是,一切似乎都不正常。我日夜努力。现在我对多线程有了更多的了解。如何获得预期的输出?有人能告诉我哪一部分我做错了吗 这是我的测试程序 这是我的股票课 这是我的买线程序 我期待这样的输出(我运行时出错: Tracking of Stock : FCS 8 shares has been brought at $37.61 Total sha

我正在做多线程。我无法获得我想要的输出。我没有编译错误。但是我不能得到我想要的结果。有人能给我建议吗

这是我的测试程序



好的,现在我得到了我的输出,但是,一切似乎都不正常。我日夜努力。现在我对多线程有了更多的了解。如何获得预期的输出?有人能告诉我哪一部分我做错了吗

这是我的测试程序


这是我的股票课


这是我的买线程序


我期待这样的输出(我运行时出错:

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
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

您正在传入
rand1
rand2
,它们属于
Random
类型


您的
BuyThread
构造函数被定义为使用
int
double

我们来看看所涉及的类型,
share
cost
参数分别是
int
double
rand1
rand2
随机的
。您期望如何你希望发生什么?(提示:查看
Random
上的可用方法)还请注意,使用
double
表示财务值是一个坏主意。请使用
BigDecimal
或整数美分。还请注意,尽管有标记,这里的问题与多线程无关。并且标题没有对您的问题给出任何有意义的提示这是我的多线程程序的一部分,我没有我不想全部显示,因为我想在安装所有程序之前一部分一部分地学习。很抱歉,我标记错误。在测试类中,我需要声明rand1为int,rand2为double,对吗?
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) {
        }
    }
}
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;
}

}
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)
{}
}
}
}
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