Java Multithreading (1)--Creating and threading information fetching

Source: Internet
Author: User

By creating a class that implements the Runnable interface. Use the thread constructor with parameters to create the thread object. This parameter is an object of the class that implements the Runnable interface.

Creates 10 threads, each of which prints a result multiplied by 1-10.

Package Com.concurrency;public class Calcalator implements Runnable{private int number;public calcalator (int number) { This.number = number;} @Overridepublic void Run () {for (int i = 1; i <=; 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 <=; i++) {Calcalator calculator = new Calcalator (i); Thread thread = new Thread (calculator); Thread.Start ();}}}

Operation Result:



Getting and setting the thread information:

ID: A unique identifier for the thread was saved.

Name: Names of the threads are saved.

Priority: The precedence of the thread object. 1-10, where 1 is the lowest priority.

Status: The state of the thread is saved. 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 Ca Lculator (int number) {This.number=number;} /** * Method that does the calculations */@Overridepublic 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.M in_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[];//creates an array of objects of size 10, setting different numbers for each object. To construct the thread object. Set 5 of these priority levels to the highest 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);//Set the name for each thread}//Wait for the finalization of the threads. Meanwhile,//write the status of those threads in a filetry (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 ();} When the state of any one thread changes, we write him to the file. 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 ();}} Used to write the thread ID, name, priority, old state and new state. private staticvoid Writethreadinfo (PrintWriter pw, thread thread, state state) {pw.printf ("Main:id%d-%s\n", Thread.getid (), thread.ge Tname ());p w.printf ("main:priority:%d\n", thread.getpriority ());p w.printf ("Main:old State:%s\n", state);p w.printf ( "Main:new State:%s\n", Thread.getstate ());p w.printf ("Main: ************************************\n");}}
The results are as follows:



Java Multithreading (1)--Creating and threading information fetching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.