2 Replies - 334 Views - Last Post: 15 May 2012 - 02:06 PM Rate Topic: -----

#1 raihan26  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Percentage of calls go to CommandA, CommandB, CommandC

Posted 15 May 2012 - 12:08 PM

This is my Config File(Test.txt)
CommandA   75%
CommandB   15%
CommandC   10%


Question:-
The above Config File have some type of weighing to the Commands (execute CommandA 75% of the time, execute CommandB 15% of the time and execute Command C 10% of the time. Total weighing should add up to 100%.

Code that I wrote:-
I wrote a multithreading program in which I am reading the files line by line but not sure how should I do the above question in which this much percentage of random calls go to CommandA, and this much percentage of random calls go to CommandB and same with CommandC.

public static void main(String[] args) {
    
    		for (int i = 1; i <= threadSize; i++) {
    			new Thread(new ThreadTask(i)).start();
    		}
    	}

class ThreadTask implements Runnable {
    	private int id;
    
    	public ThreadTask(int id) {
    		this.id = id;
    	}
    
    	public synchronized void run() {
    		BufferedReader br = null;
    
    		try {
    			String sCurrentLine;
    
    			br = new BufferedReader(new FileReader("C:\\testing\\Test.txt"));
    
    			while ((sCurrentLine = br.readLine()) != null) {
    				String[] s = sCurrentLine.split("\\s+");
    				for (String split : s) {
					if(split.matches("\\d*"))
					System.out.println(split);
				}
			}
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (br != null)br.close();
    			} catch (IOException ex) {
    				ex.printStackTrace();
    			}
    		}
    
    	}
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Percentage of calls go to CommandA, CommandB, CommandC

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Percentage of calls go to CommandA, CommandB, CommandC

Posted 15 May 2012 - 12:59 PM

Your ThreadTask class will need to record the number of lines it processed. You'll want a public accessor to get that line count and maybe a flag to know when it's done. e.g.
ThreadTask [] tasks = new ThreadTask[threadSize]; // you want to keep these
for (int i = 0; i < threadSize; i++) {
	tasks[i] = new ThreadTask(i+1);
	new Thread(tasks[i]).start();
}
// wait for everything to finish
// look at your list
int total = 0;
for(ThreadTask task : tasks) {
	int count = task.getLineCount();
//...


Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,604
  • Joined: 27-December 08

Re: Percentage of calls go to CommandA, CommandB, CommandC

Posted 15 May 2012 - 02:06 PM

This is really more of a help question than advanced discussion, so I'll move this to Java Help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1