目录
一 进程和线程定义
二 创建线程的种类
(1)继承java.lang.Thread
(2)实现java.lang.Runnable接口
三 多线程
(1)继承java.lang.Thread多线程
(2)实现java.lang.Runnable接口多线程
四 线程的状态
五 线程优先级
六 线程休眠
七 使用线程示例
进程:应用程序的执行实例
线程:CPU调度和分派的基本单位,进程中执行运算的最小单位
a)定义MyThread类继承Thread类
b)重写run()方法,编写线程执行体
c)创建线程对象,调用start()方法启动线程
package org.example.demo01;
//步骤1:定义Demo01类继承Thread类
public class Demo01 extends Thread{
//步骤2:重写run()方法,编写线程执行体
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
}
public static void main(String[] args) {
//步骤3:创建线程对象,调用start()方法启动线程
Demo01 demo01 = new Demo01();
demo01.start();
}
}
a)定义MyRunnable类实现Runnable接口
b)实现run()方法,编写线程执行体
c)创建线程对象,调用Thread类的start()方法启动线程
package org.example.demo01;
//步骤1:定义MyRunnable类实现Runnable接口
public class Demo03 implements Runnable{
//步骤2:实现run()方法,编写线程执行体
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
}
public static void main(String[] args) {
//步骤3:创建线程对象,调用Thread类的start()方法启动线程
Demo03 demo03 = new Demo03();
Thread demo03Thread = new Thread(demo03);
demo03Thread.start();
}
}
核心要点:多个线程之间存在交替执行,不是真正的并行
package org.example.demo01;
public class Demo02 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public static void main(String[] args) {
Demo02 a1 = new Demo02();
Demo02 a2 = new Demo02();
a1.start();
a2.start();
}
}
package org.example.demo01;
public class Demo04 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
}
public static void main(String[] args) {
Demo04 demo04 = new Demo04();
Thread myThread01 = new Thread(demo04);
Thread myThread02 = new Thread(demo04);
myThread01.start();
myThread02.start();
}
}
线程优先级由1~10表示,1最低,默认优先级为5
优先级高的线程获得CPU资源的概率较大
但是这个优先级已经被淘汰了,基本上没有人设置这个使用,因为它太过不稳定,比如说优先级为 10 的可能都抢不过优先级为 1 的
package org.example.demo01;
public class Demo05 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
}
public static void main(String[] args) {
Demo05 demo05 = new Demo05();
Thread a1 = new Thread(demo05,"第一个线程");
Thread a2 = new Thread(demo05,"第二个线程");
System.out.println("默认的线程优先级:"+a1.getPriority());
a1.setPriority(1);
a2.setPriority(10);
System.out.println("第一个线程优先级:"+a1.getPriority());
System.out.println("第二个线程优先级:"+a2.getPriority());
a1.start();
a2.start();
}
}
核心要点:sleep()可以是线程阻塞【休眠】
package org.example.demo01;
public class Demo06 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i == 6){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
}
public static void main(String[] args) {
Demo06 demo06 = new Demo06();
Thread a1 = new Thread(demo06);
a1.start();
}
}
同步代码块 synchronized
核心要点:在非集群状态下,使用synchronized可以保持线程的安全,如果是集群状态下,则synchronized会失效
package org.example.demo02;
public class Site implements Runnable{
private int count = 100;
private int num = 0;
@Override
public void run() {
while (true){
synchronized (Site.class) {
if (count <= 0){
break;
}
num++;
count--;
System.out.println(Thread.currentThread().getName()+"抢到了第"+num+"张票,还剩"+count+"张票");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package org.example.demo02;
public class Test {
public static void main(String[] args) {
Site site = new Site();
Thread thread1 = new Thread(site,"乔尼");
Thread thread2 = new Thread(site,"杰洛");
Thread thread3 = new Thread(site,"大总统");
System.out.println("**************");
thread1.start();
thread2.start();
thread3.start();
}
}