Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Java MutiThreading Stock

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java MutiThreading Stock

    I am doing a multi threading program . I am unable to get the output i want. I have no compilation error . But I cant get the result I , Null exception. Can someone please advise me ?

    This is my Test program

    ----------
    import java.util.*;
     
    public class Test
    {
     
     
    public static void main (String[]args)
    {
    int totalShares = 0 ;
    double totalCost = 0.0;
    String name = "FCS";
    double profit = 0.0;
    int rand1 = 0;
    double rand2 = 0.0;
    Stock stock ;
    double getprofit = 0.0 ;
    double currentPrice = 36.00 ;
    Thread bt[] = new Thread [2];
    Thread st[] = new Thread [2];
    //Stock stock = new Stock();
     
    for (int i=0; i < 2; i ++)
    {
    bt[i] = new BuyThread (rand1, rand2);
    bt[i].start() ;
    }
     
    for (int i=0; i < 2; i ++)
    {
    st[i] = new SellThread (rand1, rand2);
    st[i].start() ;
    }
     
    for (int i=0; i < 2; i ++)
    {
    try
    {
    st[i].join();
    bt[i].join();
    }
    catch (InterruptedException e)
    {}
    }
     
    System.out.println ("Total shares now " +totalShares + " at total cost" +totalCost ); 
    System.out.printf ("At$ 36.00 per share, profit is " +Stock.getprofit);
    }
    }

    This is my Stock Class

    ----------
    import java.util.*;
    public class Stock
    {
       String name;
       int totalShares;
       double totalCost;
       double totalProfitLoss;
     
     
        public Stock(String stockName)
        {
            name = stockName;
            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 getProfit (double currentPrice)
        {
            totalProfitLoss = currentPrice * totalShares;
            return totalProfitLoss - totalCost;
        }
    }

    This is my BuyThread program


    ----------
     
    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()
    {
    for (int j = 0 ; j < 5; 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 (100);
    }
    catch (InterruptedException e)
    {}
    }
    }
    }

    This is my SellThread

    ----------
     
    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++)
    {
    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) ;
     
    try
    {
    Thread.sleep(60);
    }
    catch (InterruptedException e)
    {}
    }
    }
    }
    I am expecting a output like this (Which I have error running:

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Java MutiThreading Stock

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    Too much unformatted code. Please correct according to the instructions in the link I provided, and then add a new post to let us know. Thanks.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java MutiThreading Stock

    Thank you. Is this the correct now?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,043
    My Mood
    Mellow
    Thanks
    184
    Thanked 648 Times in 635 Posts

    Default Re: Java MutiThreading Stock

    Better. The code you posted does not compile due to this line:

    System.out.printf ( "At$ 36.00 per share, profit is " + Stock.getprofit );

    What should it be?

    Edit: And if I delete that line, then there are 3 or 4 other NPEs later in the code. Please correct and give us code we can use to duplicate the error you want help with.

    Further, if you're expecting to be able to predict the order in which the threads will execute and produce results, your expectations are not consistent with Java design.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java MutiThreading Stock

    Did I do my buyThread and sellThread correctly? I know that my Test class is done wrongly.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java MutiThreading Stock

    you are calling the methods on "stock" but it is not refrencing to any object.
    whenever you need to call a method you hav to first create an object and give the refrence to it.
    Stock stock;
    stock.doSomething();
    is worng.
    Stock stock =new Stock(); // refrence to stock object
    and then u call methods the refrence variable
    stock.doSomething

Similar Threads

  1. Mutithreading problem
    By ashish12169 in forum Threads
    Replies: 1
    Last Post: January 8th, 2013, 06:30 AM
  2. stock charts...
    By theChameleon in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 04:11 AM
  3. stock screener api java
    By thosecars82 in forum Java SE APIs
    Replies: 0
    Last Post: January 31st, 2010, 08:50 AM
  4. Replies: 1
    Last Post: October 20th, 2009, 06:31 AM
  5. Java stock project help
    By lotus in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 12th, 2009, 04:16 AM

Tags for this Thread