码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程(1)--创建和线程信息获取

时间:2014-11-21 16:13:51      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   使用   sp   java   for   

通过创建实现Runnable接口的类。使用带参数的Thread构造器来创建Thread对象。这个参数就是实现Runnable接口的类的一个对象。

创建10个线程,每个线程打印乘以1-10的结果。

package com.concurrency;

public class Calcalator implements Runnable{

	private int number;
	public Calcalator(int number) {
		this.number = number;
	}
	@Override
	public void run() {
		for(int i = 1; i <= 10; i++){
			System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), 
							number, i, i * number);
		}
	}
	public static void main(String[] args) {
		for(int i = 1; i <= 10; i++){
			Calcalator calculator = new Calcalator(i);
			Thread thread = new Thread(calculator);
			thread.start();
		}
	}
}

运行结果:

bubuko.com,布布扣


线程信息的获取和设置:

ID:保存了线程的唯一标示符。

Name:保存了线程的名称。

Priority:线程对象的优先级。1-10,其中1是最低优先级。

Status:保存了线程的状态。 new  runnable  blocked  waiting timewaiting  terminated

package com.concurrency;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Thread.State;

/**
 * This class prints the multiplication table of a number
 *
 */
public class Calculator implements Runnable {

	/**
	 *  The number
	 */
	private int number;
	
	/**
	 *  Constructor of the class
	 * @param number : The number
	 */
	public Calculator(int number) {
		this.number=number;
	}
	
	/**
	 *  Method that do the calculations
	 */
	@Override
	public void run() {
		for (int i=1; i<=10; i++){
			System.out.printf("%s: %d * %d = %d\n",Thread.currentThread().getName(),number,i,i*number);
		}
	}
	public static void main(String[] args) {

		// Thread priority infomation 
		System.out.printf("Minimum Priority: %s\n",Thread.MIN_PRIORITY);
		System.out.printf("Normal Priority: %s\n",Thread.NORM_PRIORITY);
		System.out.printf("Maximun Priority: %s\n",Thread.MAX_PRIORITY);
		
		Thread threads[];
		Thread.State status[];
		
		//创建大小为10的对象数组,为每个对象设置不同的数字。来构造线程对象。将其中的5个优先级设为最高
		threads=new Thread[10];
		status=new Thread.State[10];
		for (int i=0; i<10; i++){
			threads[i]=new Thread(new Calculator(i));
			if ((i%2)==0){
				threads[i].setPriority(Thread.MAX_PRIORITY);
			} else {
				threads[i].setPriority(Thread.MIN_PRIORITY);
			}
			threads[i].setName("Thread "+i);//为每一个线程设置名字
		}
		
		
		// Wait for the finalization of the threads. Meanwhile, 
		// write the status of those threads in a file
		try (FileWriter file = new FileWriter("F:\\log.txt");PrintWriter pw = new PrintWriter(file);){
			
			for (int i=0; i<10; i++){
				pw.println("Main : Status of Thread "+i+" : "+threads[i].getState());
				status[i]=threads[i].getState();
			}

			for (int i=0; i<10; i++){
				threads[i].start();
			}
			//任何一个线程的状态发成了变化,我们就将他写入到文件中。
			boolean finish=false;
			while (!finish) {
				for (int i=0; i<10; i++){
					if (threads[i].getState()!=status[i]) {
						writeThreadInfo(pw, threads[i],status[i]);
						status[i]=threads[i].getState();
					}
				}
				
				finish=true;
				for (int i=0; i<10; i++){
					finish=finish &&(threads[i].getState()==State.TERMINATED);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//用来写下线程的ID, 名称, 优先级, 旧的状态和新的状态.
	private static void writeThreadInfo(PrintWriter pw, Thread thread, State state) {
		pw.printf("Main : Id %d - %s\n",thread.getId(),thread.getName());
		pw.printf("Main : Priority: %d\n",thread.getPriority());
		pw.printf("Main : Old State: %s\n",state);
		pw.printf("Main : New State: %s\n",thread.getState());
		pw.printf("Main : ************************************\n");
	}

}
结果如下:

bubuko.com,布布扣

Java多线程(1)--创建和线程信息获取

标签:blog   http   io   ar   os   使用   sp   java   for   

原文地址:http://blog.csdn.net/yapian8/article/details/41349201

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!