pro46.java

class SumThread extends Thread { public SumThread(int from, int to) { this.from = from; this.to = to; sum = 0; } public void run( ) { for(int i = from; i <= to; i++) { sum += i; } } public int getSum( ) { return sum; } public int getAvg( ) { return(sum/5); } private int from, to, sum; } public class pro46 { public static void main(String args[])throws Exception { SumThread t1 = new SumThread(1, 5); SumThread t2 = new SumThread(6, 10); SumThread t3 = new SumThread(11, 15); t1.start( ); t2.start( ); t3.start( ); t1.sleep(1000);//thread sleep for 1000 ml second System.out.println("sum of thread 1 is "+t1.getSum( )+" and avg is "+t1.getAvg()); t2.sleep(3000);//thread sleep for 3000 ml second System.out.println("sum of thread 2 is "+t2.getSum( )+" and avg is "+t2.getAvg()); t3.sleep(5000);//thread sleep for 5000 ml second System.out.println("sum of thread 3 is "+t3.getSum( )+" and avg is "+t3.getAvg()); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.