java 多线程

本文详细阐述了进程与线程的概念,重点介绍了线程的状态、创建与启动方式,以及如何通过多线程提升程序性能。此外,文章还讲解了线程的优先级、同步机制和线程之间的通讯方法,如等待与通知。最后,通过生产者-消费者模型实例展示了线程同步的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、线程与进程

进程是程序的一次动态执行过程。

线程是进程中的一个执行流程,一个进程可以运行多个线程,线程是多任务处理环境中最小的处理单位。

进程-重量级任务-每个进程分配独立的地址空间

线程-轻量级任务-共享相同地址空间并且分享同一个进程

二、线程的状态

为什么要使用多线程

因为读取本地系统资源的速度远低于CPU处理能力,所以多线程帮助你写出CPU最大利用率的高效程序,CPU空闲时间保持最低。




三、主线程

java应用总是从main()方法开始运行,main()方法运行在一个线程内,这个线程被称为主线程。

作用:(1)产生其他子线程

            (2)关闭线程

四、创建、启动线程

1、Thread 线程类

新建一个类继承Thread类,并覆盖Thread的run()方法。
public class MyThread extends Thread {

	public void run() {
		for (int n = 0; n < 5; n++) {
			try {
				Thread.sleep(1000);// 线程休眠1秒
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("已经过去了" + n + "秒");
		}
	}
}

public class Test {
	public static void main(String args[]) {
		Thread t = new MyThread();
		t.start();// 启动线程
	}

}

2、Runnable 接口

新建一个类实现Runnable接口,并且实现run()方法.
public class MyRunnable implements Runnable {
	String name;
	Thread t;

	public MyRunnable(String ThreadName) {
		name = ThreadName;
		t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
		Thread t = Thread.currentThread();
		for (int n = 5; n > 0; n--) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("线程名" + name + ":" + t.getName() + ",倒数:" + n);
		}
		System.out.println("倒数完毕");
	}
}

public class Test {
	public static void main(String args[]) {
		Thread t = new Thread(new MyRunnable("one"));// 将Runnble作为参数传入
	}

}

五、线程结束与等待

1、isAlive()方法 

线程如果处于运行沢返回值为true,反之为false.

2、join() 方法

把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程。

六、线程优先级

线程的优先级高低是获得系统调用、CPU资源几率的高低, 并不保证该线程一定先执行
.setPriority(int level)方法设置线程优先级,优先级0<=level<=10,默认为5

七、线程同步

当两个或两个以上的线程需要共享资源,它们需要某种方法来确定资源在某一刻仅被一个线程占用。达到此目的的过程叫做同(synchronization)。

同步关键字synchronized

1、方法同步

在访问修饰符与返回值类型直接加入 关键字 synchronized
public class MyRunnable implements Runnable {
	// String name;
	Thread t;
	Call call;
	String word;

	public MyRunnable(String ThreadName, int level, Call call) {
		this.call = call;
		// name = ThreadName;
		t = new Thread(this);
		t.setPriority(level);
		t.start();
	}

	public MyRunnable(Call call, String word) {
		this.call = call;
		this.word = word;
		t = new Thread(this);
		t.start();
	}

	public void run() {
		call.caller(word);
	}
}


public class Call {
	public synchronized void caller(String word) {//方法头添加关键字<span style="font-family: Arial, Helvetica, sans-serif;">synchronized </span>

		System.out.print("[" + word);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("]");
	}
}

public class Test {
	public static void main(String args[]) {
		Call call = new Call();
		Thread t1 = new Thread(new MyRunnable(call, "hello"));
		Thread t2 = new Thread(new MyRunnable(call, "world"));
	}

}
结果为:[hello]
             [world]

2、语句同步

将调用的方法放入一个synchronized块内。
public class MyRunnable implements Runnable {
	// String name;
	Thread t;
	Call call;
	String word;

	public MyRunnable(String ThreadName, int level, Call call) {
		this.call = call;
		// name = ThreadName;
		t = new Thread(this);
		t.setPriority(level);
		t.start();
	}

	public MyRunnable(Call call, String word) {
		this.call = call;
		this.word = word;
		t = new Thread(this);
		t.start();
	}

	public void run() {
		synchronized (call) {//添加 synchraoized (Object)
			call.caller(word);
	}
}
public class Call {
	public void caller(String word) {
		System.out.print("[" + word);
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("]");
	}
}
public class Test {
	public static void main(String args[]) {
		Call call = new Call();
		Thread t1 = new Thread(new MyRunnable(call, "hello"));
		Thread t2 = new Thread(new MyRunnable(call, "world"));
	}

}


八、线程之间通讯

wait()方法 使线程进入等待状态

notify()方法 唤醒处于等待状态的线程

例如:生产者-消费者模型
         (1)生产者生产产品,消费者消费产品
         (2)生产者效率大于消费者消费效率,生产者生产数量足够的产品后进入等待
         (3)消费者效率大于生产者生产效率,消费完产品后消费者进入等待状态,待生产者继续生产后消费者在启动消费
         (4)消费者效率等于生产者效率,两者皆不进入等待状态,一直运行
<pre name="code" class="java">public class Product {
	int number;//产品编号
}
public class ProductBox {
	LinkedList<Product> set = new LinkedList<Product>();

	public synchronized void put(int number) {
		if (set.size() >= 5) {
			try {
				System.out.println("生产者进入等待状态");
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		Product pro = new Product();
		pro.number = number;
		set.addLast(pro);
		// this.number = number;
		// set.add(this);
		System.out.println("生产者生产了编号为:[" + number + "]的产品");
		notify();
	}

	public synchronized void get() {
		if (set.size() == 0) {
			try {
				System.out.println("消费者进入等待状态");
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		Product pro = set.removeFirst();
		System.out.println("消费者消费了编号为:[" + pro.number + "]的产品");
		notify();
	}
}



 
 
public class Producer implements Runnable {
	ProductBox pro;
	Thread t;

	public Producer(ProductBox pro) {
		this.pro = pro;
		t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
		int i = 0;
		while (true) {
			i++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			pro.put(i);
		}
	}

}

public class Consumer implements Runnable {
	ProductBox pro;
	Thread t;

	public Consumer(ProductBox pro) {
		this.pro = pro;
		t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
		while (true) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			pro.get();
		}
	}

}
public class Test {

	public static void main(String[] args) {
		ProductBox pro = new ProductBox();
		Producer proer = new Producer(pro);
		Consumer coner = new Consumer(pro);
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值