多线程
来源:互联网 发布:mac照片怎么导入手机 编辑:程序博客网 时间:2024/06/03 21:38
多线程
1. 多线程的创建
1.用extends继承Thread类来重写父类的run方法
例如:
Public class Mythread extends Thread{
Public void run(){
System.out.println(“run方法被执行”);
}
Public static void main(String[] args){
new Mythread().start();
}
}
执行结果:run方法被执行
2. 实现Runnable接口
Public class Mythread implements Thread{
Public void run(){
System.out.println(“run方法被执行”);
}
Public static void main(String[] args){
Mythread thread = new Mythread();
new Thread(thread).start();
}
}
执行结果:run方法被执行
3. 匿名内部类
Public static void main(String[] args){
new Thread(){
Public void run(){
System.out.println(“run方法被执行”);
}.start();
}
}
执行结果:run方法被执行
2. 线程的生命周期及状态转换
3. 线程的休眠
Thread中的静态方法sleep(),让当前正在执行的线程暂停一段时间,进入阻塞状态,当休眠时间已过,就进入就绪状态
4. 线程的同步
关键字:synchronized
同步代码块 synchronized(lock){} //lock可以是任意对象
例如:synchronized(lock){
System.out.println(“同步代码块被执行”);
}
同步方法 public synchronized 返回类型 方法名(参数){}
例如: public synchronized void show(){
System.out.println(“同步方法被调用”);
}
Class Ticket1 implements Runnable{
Private int tickets = 5;
Public void run(){
While(true){
saleTicket();
If(tickets<=0){
break;
}
}
}
Private synchronized void saleTicket(){
If(tickets){
Try{
Thread.sleep(10);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+”---卖出的票”+tickets--);
}
}
}
Public class Text{
Public static void main(String[] args){
Tickets tickets = new Tickets();
new Thread(tickets,”线程一”).start();
new Thread(tickets,”线程二”).start();
}
}
执行结果: 线程一---卖出的票5
线程一---卖出的票4
线程二---卖出的票3
线程二---卖出的票2
线程一---卖出的票1
5. 多线程的通信
void wait(): 使当前线程放弃同步锁进入等待,当调用notify()或者notifyAll()方法唤醒该线程为止
void notify(): 唤醒此同步锁上等待的第一个调用wait()方法的线程
void notifyAll(): 唤醒此同步锁上调用wait()方法的所有线程
经典例子:生产者与消费者问题 当产品数为0时,消费者等待,生产者生产。当产品数为1时,消费者消费,生产者等待。
//消费者
public class customerextends Thread {
public ArrayList<Phone>list;
public customer(ArrayList<Phone>list) {
this.list =list;
}
public void run() {
while (true) {
synchronized (list) {
if (list.size() == 0) {
try {
list.wait();
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Phone iphone = list.get(0);
list.remove(iphone);
System.out.println("消费者消费了一台" +iphone.type);
list.notify();
try {
Thread.sleep(1000);
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//生产者
public class Productextends Thread {
public ArrayList<Phone>list;
public int type;
public Product(ArrayList<Phone>list) {
this.list =list;
}
public void run() {
while (true) {
synchronized (list) {
if (list.size() == 1) {
try {
list.wait();
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Phone iphone = new Phone();
iphone.type ="苹果手机" +type++;
list.add(iphone);
System.out.println("生产者生产了一台" +iphone.type);
list.notify();
try {
Thread.sleep(1000);
} catch (InterruptedExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//产品
public class Phone {
public String type;
}
public class Test {
public static void main(String[]args) {
ArrayList<Phone> list = new ArrayList<Phone>();
new Product(list).start();
new customer(list).start();
}
}
执行结果:
注意事项:1)wait和notify必须在同步锁之内使用
2)同步锁锁定对象和wait、notify对象必须同一个
3)当对象wait挂起状态时候是会释放同步锁的
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- 多线程
- ue4 3dui交互相关
- retrofit 如何post json给服务端
- 【工作笔记】微信公众号vm页面循环遍历
- Windows Server 2016远程桌面服务配置和授权激活
- java中for和foreach循环
- 多线程
- BZOJ 1419 DP 解题报告
- js中的四种调用,this、that
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!
- 高维数据可视化
- RecyclerView的案例和解析
- Android组件学习笔记(连接互联网)
- tf.contrib.layers.xavier_initializer
- BZOJ3503