Java multi-threaded applications (numbers match)

Simple to use multi-threaded Java, 1000 generates random numbers within 1000, and then enter the number you want to find, the location of the number that appears at the output.

operation result:

 Specific ideas:

Establish interface, generating 1000 random numbers, and these numbers appear in the text field, then build ten threads, and then they were taken to find 1-100,101-200. . . 801-900,901-1000 ten range. To find the digital experience to put in the collection, storage position to the final output text box.

Code:

public class Main {
	public static void main(String[] args) {
		
		Face_JM face=new Face_JM();
		face.Star();
	}
}	

/*
 * 界面设计
 */
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Face_JM extends JFrame implements ActionListener{
	JButton button1=new JButton("产生1000个随机数");
	JButton button2=new JButton("查找");
	TextArea area=new TextArea(10,20);
	TextField field1=new TextField();
	TextField field2=new TextField("-1");
	int[] a=new int[1001];
	public Face_JM() {}
	
	public void Star(){
		this.setBounds(200, 100, 500, 400);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setLayout(null);
		
		button1.setBounds(20, 10, 150, 20);
		add(button1);
		button1.addActionListener(this);
		button2.setBounds(20, 250, 100, 20);
		add(button2);
		button2.addActionListener(this);
		
		area.setBounds(20, 40, 440, 200);
		add(area);
		
		field1.setBounds(180, 250, 100, 20);
		add(field1);
		
		field2.setBounds(180, 300, 100, 20);
		add(field2);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==button1) {//为产生随机数按钮添加监听事件
			RandomDemo  rand=new RandomDemo();
			area.setText("");
			a=rand.SetRand();
			//把得到的数组转化成字符串,输出到文本域
			for(int i=1;i<=1000;i++) {
				area.append(String.valueOf(a[i])+",");
				if(i%50==0) {
					area.append("\n");
				}
			}
		}else if(e.getSource()==button2) {//为查找按钮添加监听事件
			String sc=field1.getText();
			lookUp look=new lookUp(sc, a);
			field2.setText(look.thread());
		}
	}
}

/*
 * 产生1000个随机数
 */
import java.util.Random;

public class RandomDemo {
	int[] array =new int[1001];
	public RandomDemo() {
		Random rd=new Random();
                //获取1000个随机数,并存到数组里
		for(int i=1;i<=1000;i++) {
			array[i]=rd.nextInt(1000);
		}
	}
	
	public int[] SetRand() {
		return array;
	}
}

/*
 * 中间类,创建线程
 */
import java.util.ArrayList;

public class lookUp {
	String sc;
	int[] a=new int[1001];
	public lookUp(String sc,int[] a) {
		this.sc=sc;
		this.a=a;
	}
	
	public String thread() {
		String result="";
		Thread num;
		seek s=new seek();
		s.Setarray(a);
		s.setSc(sc);
		for(int i=0;i<10;i++) {	
                        //建立线程
			num=new Thread(s);
			num.setName("num"+i);
			num.start();		
		}
                //这里睡眠是为了等待所有的线程都结束在执行下一步,不然的话会出现有的线程还没有结束,程序就继续执行了,导致最后结果不正确
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
                //获取结果
		result=s.getResult();
                //返回结果
		return result;
	}

}

/*
 * 多线程实现类
 */

import java.util.ArrayList;

public class seek implements Runnable{
	private int[] array=new int[1001];
	private int sc;
	private String result="";
	ArrayList<Integer> re;
	public seek() {
		re=new ArrayList<Integer>();
	}
	
	public void Setarray(int[] array) {
		this.array=array;
	}
	
	public void setSc(String sc) {
		this.sc=Integer.valueOf(sc);
	}
	
	@Override
	public void run() {
		// TODO 自动生成的方法存根
		String name=Thread.currentThread().getName();
                //获取线程名的最后一位
		String a=name.substring(name.length()-1);
		int t=Integer.valueOf(a);
                //设置查找的起点和终点
		int begain=1000/10*t+1;
		int end=1000/10*(t+1);
		
                //查找,并将结果存在集合内
		for(int i=begain;i<begain+99;i++) {
			if(sc==array[i]) {
				re.add(i);
			}
		}
	}
	//把集合转成字符串,最后返回结果
	public String getResult() {
		for(int i=0;i<re.size();i++) {
			result=result+re.get(i)+",";
		}
 		return result;
	}
	
}

 Basically these things, basic multi-threaded applications, if you have any questions, please private letter exchange ^. ^

 

Published 88 original articles · won praise 47 · views 10000 +

Guess you like

Origin blog.csdn.net/dai_ma_dong/article/details/103295446