目录
1.多线程简介
多线程简而言之就是,同时做不同的事,一个线程 执行一件事。
有关重要的几点:
- java中有两种方式实现多线程,第一种,继承java.lang.Thread,第二种,实现java.lang.Runnable接口。
- 不管哪种方法都要重写run方法
2.多线程常见方法
多线程常见方法:
方法名 | 解释 |
---|---|
getId() | 获取线程的标识符 |
getName() | 获取线程的名字 |
getPriority() | 获取线程的优先级 |
interrupt() | 中断线程 |
join() | 等待这个线程死亡,就是先执行这个线程的意思 |
setName() | 设置名字 |
setPriority() | 设置优先级 |
sleep() | 线程休眠 |
start() | 启动线程 |
yield() | 对调度程序的一个暗示,即当前线程愿意产生当前使用的处理器。 |
3.多线程的优先级
线程优先级有关的几点:
- Thread中有个yield()方法是线程的礼让,是一个暗示作用,就像注释一样
- 线程优先级范围是1-10,没设置就是5,
- 优先级设置方法是setPriority(int i)
- 如果设置的常数不在优先级范围就会出现异常
- 优先级就是一个线程的重要性并不一定先执行它
4.多线程的同步
线程同步注意一下几点:
- 线程同步为了防止多个线程抢占资源造成资源访问冲突问题
- 线程同步关键字是synchronize,此关键字修饰方法为同步方法
- 语法:synchronize(Object){}
- 通常将共享资源方入synchronize定义的区域中
5.练习实例
多线程的第一种实现方法:
package com.d.cn;
import java.lang.Thread;
public class Test extends Thread{
int i;
//重写run方法
public void run() {
for (i=0;i<10; i++){
System.out.println(i);
}
}
public static void main(String[] args) {
//start()方法启动线程
new Test().start();
}
}
结果:
0
1
2
3
4
5
6
7
8
9
第二种实现方法,并含有常用的方法:
package com.d.cn;
/*
这个程序的功能是两个线程,一个100一次输出0——9数字,第二个是100一次输出char数组中的前10个字符
将第二个线程在第一个线程中执行
第二个线程执行过程中进行中断
目的:
为了表现线程的第二种创建方式
理解线程启动,休眠,中断,加入方法的使用
*/
public class Test1 {
//声明两个线程
Thread threadA;
Thread threadB;
public void Test1(){
threadA=new Thread(new Runnable(){ //这里的匿名内部类可以用lambda表达式来表示下面有
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println(i);
try {
//休眠语句要用trycatch语句包裹,参数是毫秒为单位
Thread.sleep(100);
//join()方法添加线程,表示threadB线程执行完结束后threadA线程继续执行
threadB.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
//启动线程
threadA.start();
threadB=new Thread(() -> {
char[] chars={'a','b','c','d','e','f','g','h','i','j','k'};
for(int i=0;i<10;i++){
System.out.println(chars[i]);
try {
Thread.sleep(100);
if(chars[i]=='g'){
//线程中断方法
threadB.interrupt();
}
} catch (InterruptedException e) {
System.out.println("线程被中断!");
break;
}
}
});
//启动线程
threadB.start();
}
public static void main(String[] args) {
new Test1().Test1();
}
}
结果:
0
a
b
c
d
e
f
g
h
线程被中断!
1
2
3
4
5
6
7
8
9
多线程的优先级:
package com.d.cn;
/*
这个程序演示一下优先级的设置方法
结果不明显,因为优先级只是决定线程的重要性,并不代表先执行该线程
*/
public class Test2 {
char[] chars0={'a','b','c','d','e','f','g','h','i',};
char[] chars1={'A','B','C','D','E','F','G','H','I',};
Thread threadA;
Thread threadB;
Thread threadC;
Thread threadD;
public void Test2(){
//实例化4个线程
threadA=new Thread(new MyThread1());
threadB=new Thread(new MyThread2());
threadC=new Thread(new MyThread3());
threadD=new Thread(new MyThread4());
setPriority("ThreadA",10,threadA);
setPriority("ThreadB",8,threadB);
setPriority("ThreadC",5,threadC);
setPriority("ThreadD",5,threadD);
}
//设置一个设置线程名称和优先级的方法
public static void setPriority(String threadName,int priority,Thread thread){
//设置线程优先级
thread.setPriority(priority);
//设置线程名称
thread.setName(threadName);
//启动线程
thread.start();
}
//创建4个实现Runnable接口的类
private final class MyThread1 implements Runnable{
@Override
public void run() {
for (int i=0;i<9;i++){
System.out.println(i);
}
}
}
private final class MyThread2 implements Runnable{
@Override
public void run() {
for (int i=10;i<19;i++){
System.out.println(i);
}
}
}
private final class MyThread3 implements Runnable{
@Override
public void run() {
for (char c:chars0
) {
System.out.println(c);
}
}
}
private final class MyThread4 implements Runnable{
@Override
public void run() {
for (char c:chars1
) {
System.out.println(c);
}
}
}
public static void main(String[] args) {
new Test2().Test2();
}
}
结果不唯一
多线程同步:
package com.d.cn;
/*
这个程序是搞10个资源,然后由4个线程进行打印
采用了线程同步
如果不采用线程同步的话会出现打印出负数的情况,大家可以试一下。
*/
public class Test3 implements Runnable{
//表示一共有10个资源
int num=10;
//重写run方法
@Override
public void run() {
while(true){
//线程同步关键字是synchronized
synchronized (""){
if(num>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("number"+--num);
}
}
if (num==0){
break;
}
}
}
public static void main(String[] args) {
Test3 test3=new Test3();
//实例化4个线程
Thread threadA=new Thread(test3);
Thread threadB=new Thread(test3);
Thread threadC=new Thread(test3);
Thread threadD=new Thread(test3);
threadA.start();
threadB.start();
threadC.start();
threadD.start();
}
}
结果:
number9
number8
number7
number6
number5
number4
number3
number2
number1
number0