博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java Concurrency] Chapter 1 Thread Management
阅读量:7191 次
发布时间:2019-06-29

本文共 4545 字,大约阅读时间需要 15 分钟。

1. Creating and running a thread:

(1) a class implements Runnable

(2) override run()

(3) start the thread by :

(new Thread(new RunnableClass())).start();

(4) the code:

/** * Created by Min on 2/20/2017. */public class Calculator implements Runnable {    private int number;    public Calculator(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++){            Calculator calculator=new Calculator(i);            Thread thread=new Thread(calculator);            thread.start();        }    }}

2. Getting and Setting Thread Information

ID, Name, Priority and Status.

Priority: 1~10, 1 is the minimum priority, 10 is the maximum priority

status: new, runnable, blocked, waiting, time waiting, terminated

code:

System.out.println("Main : Status of Thread "+i+" : " +                    threads[i].getState()); 这行代码当中原文应该是pw.println(); 但是我打印不出来,就用system.out代替了 主要的函数是Thread.getState(), Thread.getId(), Thread.getName(), Thread.getPriority()
import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;/** * Created by Min on 2/20/2017. */public class Calculator implements Runnable {    private int number;    public Calculator(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);        }    }    private static void writeThreadInfo(PrintWriter pw, Thread            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");    }    public static void main(String[] args) {        Thread threads[]=new Thread[10];        Thread.State 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);        }        FileWriter file = null;        try {            file = new FileWriter(".\\log.txt");        } catch (IOException e) {            e.printStackTrace();        }        PrintWriter pw = new PrintWriter(file);        // log the status before the thread starts        for (int i=0; i<10; i++){            System.out.println("Main : Status of Thread "+i+" : " +                    threads[i].getState());            status[i]=threads[i].getState();        }        // start all threads        for (int i=0; i<10; i++){            threads[i].start();        }        // wait until all threads terminates        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()== Thread.State.TERMINATED);            }        }    }}

3. Interrupting a thread

A Java program with more than one execution thread only finishes when the execution of all of

its threads end (more specifically, when all its non-daemon threads end its execution or when
one of the threads use the System.exit() method).

One peculiarity of this mechanism is that Thread has to check if it has been interrupted or

not, and it can decide if it responds to the finalization request or not. Thread can ignore it
and continue with its execution.

一般情况下,所有线程结束之后或者一个程序调用了System.exit()之后才会结束,但是java提供了finish某一个线程的方法。

一种方式是线程会定期检查有没有要finish它,线程可以决定它是否愿意被finish

/** * Created by Min on 2/20/2017. */public class PrimeGenerator extends Thread {    private boolean isPrime(long number) {        if (number <=2) {            return true;        }        for (long i=2; i

问题1:

Thread的六个state分别有什么差别?

问题2:

Thread 和 Runnable 这两个Interface 有什么差别?

问题3:

 

转载于:https://www.cnblogs.com/Gryffin/p/6423418.html

你可能感兴趣的文章
海量用户积分排名算法探讨【转载】
查看>>
Android常见工具类封装
查看>>
python 怎么模拟加header(如User-Agent、Content-Type等等)
查看>>
GridView EmptyDataTemplate 动态显示
查看>>
反素数(暴力)
查看>>
Linux关于watch的用法
查看>>
Memcache基础教程
查看>>
JS判断手机浏览器
查看>>
Python之SQLAlchemy学习--外键约束问题
查看>>
MapReduce的InputFormat过程的学习
查看>>
ZOJ 3795 Grouping 求最长链序列露点拓扑
查看>>
选择合适的缓动函数
查看>>
【mysql】数据库使用的一些规范
查看>>
JSP内置对象Session
查看>>
Java TCP Swing聊天程序
查看>>
MFC绘制棋盘格
查看>>
关于PYTHON的反射,装饰的练习
查看>>
java用字符写字符
查看>>
ASP.NET MVC5+EF6+EasyUI 后台管理系统(53)-工作流设计-我的批阅
查看>>
基于am3358的led跑马灯測试
查看>>