- 继承Thread重写Run方法
- run()方法用来包含那些被线程执行的代码
- run():仅仅是封装被线程执行的代码,直接调用是普通方法
- start():首先启动了线程,然后再由jvm去调用该线程的run()方法。
建个MyThread类
public class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<50;i++){
System.out.println(i);
}
}
}
建个测试类
public class MyThreadDemo {
public static void main(String[] args) {
MyThread my1=new MyThread();
MyThread my2=new MyThread();
// my.run();
my1.start();
my2.start();
}
}
- String getName() 返回此线程的名称。
- void setName(String name) 将此线程的名称更改为等于参数 name 。
- static Thread currentThread() 返回对当前正在执行的线程对象的引用。
建个MyThread类
public class MyThread extends Thread{
public MyThread(){}
public MyThread(String name){
super(name);
}
@Override
public void run() {
for(int i=0;i<50;i++){
System.out.println(getName()+"-"+i);
}
}
}
测试类
public class MyThreadDemo {
public static void main(String[] args) {
MyThread my1=new MyThread();
MyThread my2=new MyThread();
my1.start();
my2.start();
my1.setName("name1");
my2.setName("name2");
//构造方法改名
MyThread my1=new MyThread("name1");
MyThread my2=new MyThread("name2");
my1.start();
my2.start();
//获取主线程名
System.out.println(Thread.currentThread().getName());
}
}
线程优先级设置:
-
int getPriority() 返回此线程的优先级。
-
void setPriority(int newPriority) 更改此线程的优先级。
-
public static final int MIN_PRIORITY = 1; 优先级最小
-
public static final int NORM_PRIORITY = 5; 优先级默认
-
public static final int MAX_PRIORITY = 10; 优先级最大
-
不在范围则抛出IllegalArgumentException非法参数异常
-
static void sleep(long millis) 使当前正在执行的线程停留(暂停执行)指定的毫秒数,这取决于系统定时器和调度程序的精度和准确性。
用法:
public class Threads extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println(getName()+":"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试类
public class ThreadPriorityDemo {
public static void main(String[] args) {
Threads tp=new Threads();
Threads tp2=new Threads();
Threads tp3=new Threads();
tp.setName("name1");
tp2.setName("name2");
tp3.setName("name3");
tp.setPriority(10);
tp2.setPriority(1);
System.out.println(tp3.getPriority());
System.out.println(tp.getPriority());
System.out.println(tp2.getPriority());
tp.start();
tp2.start();
tp3.start();
}
}
- static void yield() 暂停当前正在执行的线程对象,并执行其他线程,就是礼让的意思
继承Thread
public class Threads extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println(getName()+":"+i);
Thread.yield();
}
}
}
测试类
public class ThreadYieldDemo {
public static void main(String[] args) {
Threads th=new Threads();
Threads th2=new Threads();
th.setName("老三");
th2.setName("老二");
th.start();
th2.start();
}
}
void setDaemon(boolean on)将线程标记为守护线程或者用户线程。当正在运行的线程都是守护线程时,java虚拟机退出,该方法必须在启动线程前调用
用法:
public class Threads extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName() + ":" + i);
}
}
}
测试类
public class ThreadDaemonDemo {
public static void main(String[] args) {
Threads th=new Threads();
Threads th2=new Threads();
th.setName("老三");
th2.setName("老二");
th.setDaemon(true);
th.setDaemon(true);
Thread.currentThread().setName("老大");
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName());
}
}
}
- void stop()终止线程,已过时,不建议使用
- void interrupt() 中断这个线程。
用法:
public class Threads1 extends Thread{
@Override
public void run() {
System.out.println("开始时间:"+ new Date());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("线程被中断");
}
System.out.println("结束时间:"+new Date());
}
}
测试类
public class ThreadStopDemo {
public static void main(String[] args) {
Threads1 t1=new Threads1();
t1.start();
try {
Thread.sleep(3000);
t1.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
try {
Thread.sleep(3000);
t1.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
}
运行结果:
- void join() 等待这个线程死亡,其他的线程才可以抢入
用法:
public class Threads extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName() + ":" + i);
}
}
}
测试类
public class ThreadJoinDemo {
public static void main(String[] args) {
Threads th=new Threads();
Threads th2=new Threads();
Threads th3=new Threads();
th.setName("老大");
th2.setName("老二");
th3.setName("小弟");
th.start();
try {
th.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
th2.start();
th3.start();
}
}