Results 1 to 2 of 2
  1. #1
    helioshilary is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default Expected out put is not getting in this java thread code

    Java Code:
    import java.io.*;
    import java .lang.*;
    import java.util.Random;
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.*;
    
    class CE extends Thread
    {
     public static int distribute(int lambda)
     {
      int n = 1;
      double a = 1.0;
      Random r = new Random();
      while(true)
      {
       a *= r.nextDouble();
       if(a < Math.exp((double)-lambda)) break;
       n+= 1;
      }
      return n - 1;
     }
     
     public static int expDist(int lambda)//Exponentional distribution function
     {
      return (Math.round((int)-Math.log(1-Math.random())*lambda));
     }
     
     static int num;
     static int num_p;
     static int process_t[]=new int[100];
     static String process_n[]=new String[100];
     static int wait[]=new int[100];
     static int arrival[]=new int[100];
     static int lambda,mue;
     boolean status;
     
     CE(int n,int l,int m)
     {
      num=n;
      lambda=l;
      mue=m;
      System.out.println("L "+lambda);
     }
     CE()
     {
     } 
    
     public void run()
     {
      status=true;
      //Number of process for current node
      num_p=distribute(lambda);
      //Computing arrival time
      int t=0;
      for(int i=0;i<num_p;i++)
      {
       t+=distribute(lambda);
       arrival[i]=t;
      }
      //computing processing time and process names
      for(int i=0;i<num_p;i++)
      {
       process_n[i]="N"+(num)+"-P"+(i+1);
       process_t[i]=expDist(mue);
      }
      //computing waiting times
      t=0;
      wait[0]=0;
      for(int i=1;i<num_p;i++)
      {
       if((process_t[i-1]-(arrival[i]-arrival[i-1]))>0)
        t+=(process_t[i-1]-(arrival[i]-arrival[i-1]));
       else
        t+=t;
       wait[i]=t;
      }
      System.out.println("Lambda :" + lambda);
     }
    }
    
    class Project
    {
     public static void main(String args[]) throws Exception
     {
      int non=0,count=1,l,m;
      boolean check=true;
      while(check)
      {
       try
       {
        non=Integer.parseInt(JOptionPane.showInputDialog("Input number of nodes :")); 
        check=false;
       }
       catch(Exception e)
       {
        if(count==3)
        {
         JOptionPane.showMessageDialog(null,"System will exit.");
     	System.exit(0);
        } 
        JOptionPane.showMessageDialog(null,"Enter valid input.");
        count++;
       }
      }
      CE node[]=new CE[non];
      //calculating lambda and mue for each node.
      count=0;
      for(int i=0;i<non;i++)
      {
       try
       {
        l=Integer.parseInt(JOptionPane.showInputDialog("Input lambda for Node "+(i+1)));
        m=Integer.parseInt(JOptionPane.showInputDialog("Input mue for Node "+(i+1)));
    	node[i]=new CE((i+1),l,m);
    	node[i].start();
       }
       catch(Exception e)
       {
        if(count==5)
    	{
    	 JOptionPane.showMessageDialog(null,"System will exit.");
    	 System.exit(0);
    	}    
        JOptionPane.showMessageDialog(null,"Please re-enter valid input...");
    	i--;
    	count++;
       }
      }
      
       System.out.println("################################################################################");
       System.out.println("**************************  Before load distribution  **************************");
       System.out.println("################################################################################");
       double avg1=0;    
       
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("				Node "+(node[0].num));
        System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("Number of process is : "+node[0].num_p);
        System.out.println("Lambda : "+node[0].lambda);
        System.out.println("Mue : "+node[0].mue);
        System.out.println("Pname	Arrival time	Processing time	Waiting time	Turnaround time");
        System.out.println("-------------------------------------------------------------------------------");
        double cal=0,avg=0;
        for(int j=0;j<node[0].num_p;j++)
        {
         System.out.print(node[0].process_n[j]+"\t"+node[0].arrival[j]+"\t\t"+node[0].process_t[j]+"\t\t"+node[0].wait[j]);
    	 cal=(node[0].process_t[j]+node[0].wait[j]);
         avg+=cal;
         System.out.println("\t\t"+cal);
        }
        System.out.println("Average turn around time is : "+avg/node[0].num_p+"(ms)");
        avg1+=(avg/node[0].num_p);
       
    
       
     }
    }
    In the above code i am printing the details of node 1 but it is printing the last node for any input..
    please dont be afraid of the size if program...the concept is very simple,,,
    thanks in advance
    Last edited by JosAH; 06-28-2011 at 05:36 PM. Reason: added [code] ...[/code] tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    it is printing the last node for any input..
    Is the variable that holds the data static?
    That means that there is only ONE value for that variable for all the instances of the class.
    Change the variable to non-static (remove static) and then every instance will have its own value.

Similar Threads

  1. Error Object Expected, Code 0
    By martinmilroy in forum Java Applets
    Replies: 0
    Last Post: 04-14-2011, 09:07 AM
  2. '(' expected error in JAVA HELP!
    By eel in forum New To Java
    Replies: 1
    Last Post: 09-19-2010, 11:33 AM
  3. Replies: 1
    Last Post: 05-31-2009, 10:57 PM
  4. Replies: 1
    Last Post: 05-31-2009, 02:17 PM
  5. Replies: 16
    Last Post: 05-28-2009, 03:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •