multithread.rar

  • patta
    了解作者
  • Java
    开发工具
  • 1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 1
    下载次数
  • 2016-10-19 13:15
    上传日期
finding average and min and max using multi thread in java
multithread.rar
  • multithread.java
    1.6KB
内容介绍
/* Apply Multithreading concepts in java for *sorting *finding minimum number and maximum number *average of the numbers *sum of the numbers */ import java.io.*; class SortThread extends Thread { private int a[]; private Thread t1; int i,j,temp; SortThread(int tmp[],Thread t) { a=tmp; t1=t; } public void run() { for(i=0;i<a.length;i++) { for(j=i+1;j<a.length;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } t1.interrupt(); System.out.println("Sorted list"); for (i=0;i<a.length;i++) { System.out.println(a[i]); } } } class AvgThread extends Thread { private int b[]; AvgThread(int t[]) { b=t; } public void run() { try { while(!this.isInterrupted()) Thread.sleep(1000); } catch(InterruptedException e) { //System.out.println(e); } System.out.println("Max:"+b[0]+"\tMin:"+b[b.length-1]); System.out.println("Avg:"+((b[0]+b[b.length-1])/2)); } } class TestThread { public static void main(String args[]) throws IOException { System.out.println("Enter values of n:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int num=Integer.parseInt(br.readLine()); int a[]=new int[num]; System.out.println("Get the values:"); for(int i=0;i<a.length;i++) { br=new BufferedReader(new InputStreamReader(System.in)); a[i]=Integer.parseInt(br.readLine()); } AvgThread avg=new AvgThread(a); SortThread sort=new SortThread(a,avg); sort.start(); avg.start(); } }
评论
    相关推荐