多线程常用的一些知识点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * @author Rollen-Holt
 * 取得线程的名称
 * */
class hello implements Runnable {
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName());//说明如果我们没有指定名字的话,系统自动提供名字。
        }
    }
 
    public static void main(String[] args) {
        hello he = new hello();
        new Thread(he,"A").start();
        new Thread(he,"B").start();
        new Thread(he).start();
    }
}
 
 
提醒一下大家:main 方法其实也是一个线程。在 java 中所以的线程都是同时启动的,至于什么时候,哪个先执行,完全看谁先得到 CPU 的资源。
 
在java中,每次程序运行至少启动2个线程。一个是 main 线程,一个是垃圾收集线程。因为每当使用 java 命令执行一个类的时候,实际上都会启动一个 JVM,每一个 JVM 实习在就是在操作系统中启动了一个进程。
 
判断线程是否启动   isAlive
 
join();  //强制执行该线程,直到该线程运行完成后,我们才继续执行其他的。
 
线程的优先级设置
public static void main(String[] args) {
        Thread h1=new Thread(new hello(),"A");
        Thread h2=new Thread(new hello(),"B");
        Thread h3=new Thread(new hello(),"C");
        h1.setPriority(8);//优先级最高
        h2.setPriority(2);//优先级最低
        h3.setPriority(6);
        h1.start();
        h2.start();
        h3.start();
 
    }







      本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/1893822,如需转载请自行联系原作者