Thread 类构造器
Thread()
Thread(Runnable)
Thread(Threadgroup,Runnable)
Thread(String)
Thread(Threadgroup,String)
Thread(Runnable,String)
Thread(Threadgroup,Runnable,String)
Runnable:类的实例,调用Run()方法的对象
Threadgroup:新创建的线程所属的线程组
String:表示新线程的名字
多线程的多种实现
第一种实现
class RunnableDemo extends Thread {
String name;
RunnableDemo(String name){
this.name = name;
}
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("Thread " + this.name+ " is running");
}
}
}
public class TestThread {
public static void main(String args[]) {
System.out.println("main begin");
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
System.out.println("main finish");
}
}
/*输出结果
main begin
main finish
Thread Thread-1 is running
Thread Thread-2 is running
...
线程的启动是异步的,也就是说,当你调用 start() 方法时,它不会等待线程执行完毕,而是立即返回,允许主线程继续执行。
*/
第二种实现
import java.*;
class RunnableDemo1 implements Runnable {
public void run() {
for(int i=0;i<5;i++){
System.out.println("Thread"+Thread.currentThread().getName()+" is running");
try {
Thread.currentThread().sleep(1000);
//Thread.currentThread()获取当前进程的Thread;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class TestThread {
public static void main(String args[]) {
System.out.println("main begin");
RunnableDemo1 r=new RunnableDemo1();
Thread t=new Thread(r);
t.setName("Thread1");
RunnableDemo1 r1=new RunnableDemo1();
Thread t1=new Thread(r1);
t1.setName("Thread2");
t.start();
t1.start();
System.out.println("main finish");
}
}
类Thread的常用方法
方法 | 简要说明 |
---|---|
isAlive() | 判断线程目前是否已被启动并且未被终止 |
wait() | 使当前线程处于等待状态 |
notify() | 将正在等待当前管程的线程唤醒 |
Suspend() | 暂停线程的执行(过时的) |
resume() | 被方法Suspend()暂停的线程继续执行(过时的) |
start() | 开始线程的执行 |
run() | 线程中真正执行的程序块 |
stop() | 结束线程的执行(过时的) |
sleep() | 让目前正在执行的线程小睡片刻 |
yield() | 自愿将执行的权利交给其它同优先级线程 |
join() | 暂停当前线程的执行,等待调用该方法的线程结束后在继续执行本线程 |
interrupt() | 中断线程组中所有线程或当前线程 |
currentThread() | 返回当前执行线程的引用对象 |
setName() | 设置线程的名字 |
getName() | 返回线程的名字 |
activeCount() | 返回当前线程所在线程组中活动线程的个数 |
getThreadGroup() | 返回当前线程所属的线程组名 |
setDaemon() | 设置当前线程为Daemon线程 |
isDaemon() | 测试线程是否为Daemon线程 |
toString() | 返回线程的字符串信息,包括线程的名字、优先级和线程组 |
enumerate() | 把当前线程的线程组中的活动线程拷贝到线程数组中,包括子线程 |
checkAccess() | 确定当前线程是否允许修改该线程 |
实例 |
class thread extends Thread{
thread(String name){
super(name);
}
public void run(){
for (int i=0;i<50;i++){
System.out.println("Thread 1"+getName());
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
thread t1=new thread("飞机");
thread t2=new thread("高铁");
t1.start();
t2.start();
Thread t=Thread.currentThread();
System.out.println(t.getName());
System.out.println("1111111111111");
try {
Thread.sleep(1000);
//Threead指的是当前程序正在执行的线程,这里是main线程
//注意sleep要被try-catch包围
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("1111111111111");
}
}
![[Pasted image 20240614091517.png]]
线程优先级和线程的调度
线程的调度取决于线程的优先级。
Java使用的是抢占式调度模型
随机性假如计算机只有一个 CPU,那么 CPU 在某一个时刻只能执行一条指令,线程只有得到CPU时间片,也就是使用权,才可以执行指令。所以说多线程程序的执行是有随机性,因为谁抢到CPU的使用权是不一定的
线程的优先级可以用setPriority()显式进行设置。getPriority()获得优先级。
线程优先级用整数表示。从1到10。
- Thread.MIN_PRIORITY (1)
- Thread.MAX_PRIORITY (10)
- Thread.NORM_PRIORITY (5)
但这指的是抢占的概率大小
线程同步
线程执行的随机性导致在过程中可能丢失cpu的执行权,导致出现问题,所以用线程同步解决。
同步代码块
package com.example.myPackage;
public class ThreadDemo {
public static void main(String[] args) {
thread t1=new thread();
thread t2=new thread();
t1.setPriority(10);
t1.start();
t2.start();
}
}
package com.example.myPackage;
public class thread extends Thread{
static int ticket=0;
@Override
public void run() {
while (true) {
//同步代码块
synchronized (com.example.myPackage.thread.class) {
//可以用当前的类作为锁
if (ticket > 100) {
break;
} else {
ticket++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");
}
}
}
}
}
同步方法
- 格式
修饰符 synchronized 返回值类型 方法名(方法参数) {
方法体;
}
同步静态方法的锁对象是什么呢? 类名.class
实例
package com.example.Myrunable;
public class MyRunnable implements Runnable
{
int ticket=0;
public void run()
{while (true){
boolean flag=method();
if(!flag)
break;
}
}
synchronized boolean method()
{
if(ticket<100){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
ticket++;
System.out.println(Thread.currentThread().getName()+" is selling ticket "+ticket);
return true;
}
else
{return false;}
}}
package com.example.Myrunable;
public class Demo {
public static void main(String[] args){
MyRunnable my=new MyRunnable();
Thread t1=new Thread(my);
Thread t2=new Thread(my);
t1.setName("Window 1");
t2.setName("Window 2");
t1.start();
t2.start();
}
}