Java多线程(一)
程序员文章站
2022-05-04 18:18:37
...
一、创建线程
第一种方式:继承Thread
步骤:
1,定义类继承Thread
2,复写Thread类中的 run 方法,目的是将自定义的代码存储在run方法,让线程运行
3,调用线程的start方法,该方法有两个作用:启动线程,调用run方法;
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo = new Demo();
demo.start();
for (int i = 0; i < 30; i++) {
System.out.println("main ==== "+i);
}
}
}
class Demo extends Thread{
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("demo run---- "+i);
}
}
}
运行结果如下:
main ==== 0
main ==== 1
main ==== 2
main ==== 3
main ==== 4
main ==== 5
main ==== 6
demo run---- 0
demo run---- 1
demo run---- 2
demo run---- 3
demo run---- 4
main ==== 7
main ==== 8
main ==== 9
main ==== 10
main ==== 11
main ==== 12
main ==== 13
main ==== 14
main ==== 15
main ==== 16
main ==== 17
main ==== 18
demo run---- 5
main ==== 19
main ==== 20
main ==== 21
main ==== 22
main ==== 23
main ==== 24
demo run---- 6
main ==== 25
main ==== 26
main ==== 27
main ==== 28
main ==== 29
demo run---- 7
demo run---- 8
demo run---- 9
demo run---- 10
demo run---- 11
demo run---- 12
demo run---- 13
demo run---- 14
demo run---- 15
demo run---- 16
demo run---- 17
demo run---- 18
demo run---- 19
demo run---- 20
demo run---- 21
demo run---- 22
demo run---- 23
demo run---- 24
demo run---- 25
demo run---- 26
demo run---- 27
demo run---- 28
demo run---- 29
可以看到主线程和demo线程交替运行。
如果将demo.start() 替换成demo.run,则此程序变为单线程程序,即没有启动线程,而是按顺序依次去执行方法,如:
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo demo = new Demo();
// demo.start();
//仅仅只是对象调用方法。尽管线程创建了,但是未被启动运行
demo.run();
for (int i = 0; i < 30; i++) {
System.out.println("main ==== "+i);
}
}
}
class Demo extends Thread{
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println("demo run---- "+i);
}
}
}
执行结果:
demo run---- 0
demo run---- 1
demo run---- 2
demo run---- 3
demo run---- 4
demo run---- 5
demo run---- 6
demo run---- 7
demo run---- 8
demo run---- 9
demo run---- 10
demo run---- 11
demo run---- 12
demo run---- 13
demo run---- 14
demo run---- 15
demo run---- 16
demo run---- 17
demo run---- 18
demo run---- 19
demo run---- 20
demo run---- 21
demo run---- 22
demo run---- 23
demo run---- 24
demo run---- 25
demo run---- 26
demo run---- 27
demo run---- 28
demo run---- 29
main ==== 0
main ==== 1
main ==== 2
main ==== 3
main ==== 4
main ==== 5
main ==== 6
main ==== 7
main ==== 8
main ==== 9
main ==== 10
main ==== 11
main ==== 12
main ==== 13
main ==== 14
main ==== 15
main ==== 16
main ==== 17
main ==== 18
main ==== 19
main ==== 20
main ==== 21
main ==== 22
main ==== 23
main ==== 24
main ==== 25
main ==== 26
main ==== 27
main ==== 28
main ==== 29
二、线程的几种状态:
被创建、运行、冻结、阻塞、消亡。
“阻塞” 指:具备运行资格,但没有执行权。那些处于交替获取执行权中,但还没获得执行权的线程的状态;
“冻结”指:放弃执行资格,等待被唤醒或者休眠时间还未结束的线程所处于的状态;
这五种状态的关系如图所示:
三、线程名称
线程都有自己默认的名称。格式:“Thread-编号” ,该编号从0开始。可以调用Thread的 setName() 和getName()方法
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.start();
d2.start();
for (int i = 0; i < 30; i++) {
System.out.println("main ==== "+i);
}
}
}
class Demo extends Thread{
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(this.getName()+" run---- "+i);
}
}
}
运行结果:
main ==== 0
main ==== 1
main ==== 2
main ==== 3
Thread-1 run---- 0
Thread-0 run---- 0
Thread-1 run---- 1
Thread-1 run---- 2
Thread-1 run---- 3
Thread-1 run---- 4
Thread-1 run---- 5
Thread-1 run---- 6
Thread-1 run---- 7
Thread-1 run---- 8
Thread-1 run---- 9
main ==== 4
main ==== 5
main ==== 6
main ==== 7
main ==== 8
main ==== 9
main ==== 10
Thread-1 run---- 10
Thread-0 run---- 1
Thread-0 run---- 2
Thread-0 run---- 3
Thread-0 run---- 4
Thread-0 run---- 5
Thread-0 run---- 6
Thread-0 run---- 7
Thread-1 run---- 11
main ==== 11
main ==== 12
main ==== 13
Thread-1 run---- 12
Thread-1 run---- 13
Thread-0 run---- 8
Thread-0 run---- 9
Thread-0 run---- 10
Thread-0 run---- 11
Thread-0 run---- 12
Thread-0 run---- 13
Thread-0 run---- 14
Thread-0 run---- 15
Thread-0 run---- 16
Thread-0 run---- 17
Thread-0 run---- 18
Thread-0 run---- 19
Thread-1 run---- 14
main ==== 14
main ==== 15
main ==== 16
main ==== 17
main ==== 18
main ==== 19
Thread-1 run---- 15
Thread-1 run---- 16
Thread-1 run---- 17
Thread-1 run---- 18
Thread-1 run---- 19
给线程设定名称:
public class ThreadDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d1 = new Demo("one1");
Demo d2 = new Demo("two2");
d1.start();
d2.start();
for (int i = 0; i < 20; i++) {
System.out.println("main ==== "+i);
}
}
}
class Demo extends Thread{
public Demo(String str) {
// TODO Auto-generated constructor stub
super(str);
}
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(this.getName()+" run---- "+i);
}
}
}
执行结果如下:
main ==== 0
main ==== 1
main ==== 2
main ==== 3
one1 run---- 0
two2 run---- 0
two2 run---- 1
two2 run---- 2
two2 run---- 3
two2 run---- 4
two2 run---- 5
two2 run---- 6
two2 run---- 7
two2 run---- 8
two2 run---- 9
two2 run---- 10
two2 run---- 11
two2 run---- 12
two2 run---- 13
two2 run---- 14
two2 run---- 15
two2 run---- 16
two2 run---- 17
two2 run---- 18
one1 run---- 1
main ==== 4
main ==== 5
main ==== 6
main ==== 7
main ==== 8
one1 run---- 2
one1 run---- 3
one1 run---- 4
one1 run---- 5
one1 run---- 6
one1 run---- 7
two2 run---- 19
one1 run---- 8
main ==== 9
one1 run---- 9
main ==== 10
one1 run---- 10
main ==== 11
one1 run---- 11
main ==== 12
main ==== 13
one1 run---- 12
main ==== 14
main ==== 15
main ==== 16
main ==== 17
main ==== 18
main ==== 19
one1 run---- 13
one1 run---- 14
one1 run---- 15
one1 run---- 16
one1 run---- 17
one1 run---- 18
one1 run---- 19
获取当前执行的线程:currentThread()
上一篇: java多线程学习笔记(一)
下一篇: Java基础之多线程-多生产多消费