知识点 :
多线程有很多的方法定义,但是大部分方法都是在Thread类里面。
具体内容:
1.线程的命名与取得。
由于每次运行都需要资源抢占,所以每次运行结果都是不同的。因此要区分每一个线程,那么就必须依靠线程的名字。对于线程名字一般而言会在其启动之前进行定义,不建议在启动之后更改名称,或者为不同的线程设置重名的情况。
如果要进行线程名称的操作,可以使用Thread类的如下方法:
- 构造方法:public Thread(Runnable target,Sting name);
- 设置名字:public final void setName(String name);
- 取得名字:public final String getName();
观察线程的命名:
对于线程的名字的操作会出现一个问题,这些方法属于Thread类,可以如果换回到线程类(Runnable子类),这个类里面并没有继承Thread类。如果要想取得名字,那么能取得的就是当前执行本方法的线程名字。所以在Thread类里面提供了一个方法:
- 取得当前线程对象:public static Thread currentThread().
范例:观察线程的命名
package 多线程;
class MyThread1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class TestDemo {//主类
public static void main(String[] args) throws Exception{
MyThread1 mt = new MyThread1();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
}
}
如果在实例化Thread类对象的时候没有为其设置名字,那么会自动的进行编号命名,也就是说保证线程对象的名字不重复!
观察如下的一个程序执行:
package 多线程;
class MyThread1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class TestDemo {//主类
public static void main(String[] args) throws Exception{
MyThread1 mt = new MyThread1();
new Thread(mt,"自己的线程").start();
mt.run(); //直接调用run()方法,mia
}
}
原来主方法就是一个 线程(main线程),那么所有在主方法上创建的线程实际上都可以将其表示为子线程。
通过以上的低吗可以发现,线程实际上一只都存在(主方法就是主线程),可是进程去那里了呢?
每当使用java命令去解释一个程序类的时候,对于操作系统而言,都相当于启动了一个新的进程。而main只是进程上的一个子线程而已。
提问:每一个jvm进程启动的时候至少启动几个线程呢?
- mian线程:程序的主要执行,以及启动子线程;
- gc线程:负责垃圾收集。
线程的休眠
所谓的线程休眠指的就是让线程的执行速度稍微变慢一点。休眠的方法:
- Thread类里面的方法:public static void sleep(long milis) throws InterruptedException
范例:观察睡眠的特点
package 多线程;
class MyThread1 implements Runnable{
@Override
public void run() {
for(int x=0;x<10000;x++){
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class TestDemo {//主类
public static void main(String[] args) throws Exception{
MyThread1 mt = new MyThread1();
new Thread(mt,"自己的线程A").start();
}
}
因为每一次执行到run()里面的Thread.sleep(1000)都会慢一秒,所以执行的速度会变慢。
默认情况下,在休眠的时候如果设置多个线程对象,那么所有的线程对象一起进入到run()方法(所谓的先后顺序实在是因为太短了,但是实际上是有区别的)。
线程优先级
所谓的优先级指的是越高的优先级,越有可能先执行。在Thread类里面提供有两个方法执行优先级操作:
- 设置优先级:public final void setPriority(int newPriority)
- 取得优先级:public final int getPriority()
int的取值有三种:
- 最高优先级:public static final int MAX_PRIORITY 10
- 中等优先级:public static final int NORM_PRIORITY 5
- 最低优先级:public static final int MIN_PRIORITY 1
范例:测试
package 多线程;
class MyThread1 implements Runnable{
@Override
public void run() {
for(int x=0;x<20;x++){
System.out.println(Thread.currentThread().getName()+",x="+x);
}
}
}
public class TestDemo {//主类
public static void main(String[] args) throws Exception{
MyThread1 mt = new MyThread1();
Thread t1 = new Thread(mt,"自己的线程A");
Thread t2 = new Thread(mt,"自己的线程B");
Thread t3 = new Thread(mt,"自己的线程C");
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}
范例:主线程优先及是多少?
public class TestDemo {//主类
public static void main(String[] args) throws Exception{
System.out.println(Thread.currentThread().getPriority());
}
}
主线程中等优先级。
总结:1.Thread。currentThread()可以取得当前线程类对象;
2.Thread.sleep()主要是休眠,感觉是一起休眠,但是实际上是有先后顺序的;
3.优先级越高的线程的对象越有可能先执行。