Java笔记 多线程(二)

线程的状态 

线程调度 

        线程调度指按照特定机制为多个线程分配CPU的使用权

方       法

 说       明

void setPriority(int  newPriority)

更改线程的优先级

static void sleep(long millis)

在指定的毫秒数内让当前正在执行的线程休眠

void join()

等待该线程终止

static void yield()

暂停当前正在执行的线程对象,并执行其他线程

void interrupt()

中断线程

boolean isAlive()

测试线程是否处于活动状态

线程休眠 

        让线程暂时睡眠指定时长,线程进入阻塞状态

        睡眠时间过后线程会再进入可运行状态

public class Wait {

	public static void bySec(long s) {
		for (int i = 0; i < s; i++) {
			System.out.println(i+1+"秒");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}


public class Test {

	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getName()+"线程开始");
		Wait.bySec(3);
		System.out.println(Thread.currentThread().getName()+"线程结束");
	}
}

 线程的强制运行

        使当前线程暂停执行,等待其他线程结束后再继续执行本线程

public class MyThread implements Runnable {

	@Override
	public void run() {
		for (int i = 1; i <=20; i++) {
			System.out.println(Thread.currentThread().getName()+" "+i);
		}
	}
}


public class Test {
	public static void main(String[] args) {
		MyThread mt=new MyThread();
		Thread t=new Thread(mt,"凯撒");
		t.start();
		for (int i = 0; i < 10; i++) {
			if(i==5){
				try {
					t.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}			
			System.out.println(Thread.currentThread().getName()+"运行第"+(i+1));
		}		
	}
}

线程的礼让

        暂停当前线程,允许其他具有相同优先级的线程获得运行机会

        该线程处于就绪状态,不转为阻塞状态

public class MyThread implements Runnable {

	@Override
	public void run() {
		for (int i = 1; i <=5; i++) {
			System.out.println(Thread.currentThread().getName()+"正在运行"+i);
			if(i==3){
				System.out.print("线程礼让:  ");
				Thread.yield();
			}
		}		
	}
}


public class Test {
	public static void main(String[] args) {
		MyThread mt1=new MyThread();
		Thread t1=new Thread(mt1,"卡死");
		Thread t2=new Thread(mt1,"凯撒");
		
		t1.start();
		t2.start();
	}
}

多线程共享数据引发的问题

public class Piao implements Runnable{
	int num =10;
	int count=0;
	
	@Override
	public void run() {
		while(true){
			if(num<=0){
				break;
			}
			num--;
			count++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+
					"抢到第"+count+"张票,现在还剩"+num+"张票");
		}		
	}
}


public class Test {
	public static void main(String[] args) {
		Piao p=new Piao();
		Thread t1=new Thread(p,"张飒");
		Thread t2=new Thread(p,"赵四");
		Thread t3=new Thread(p,"王红");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

发现的问题

        不是从第1张票开始

        存在多人抢到一张票的情况

        有些票号没有被抢到

同步方法(1)

用synchronized修饰的方法控制对类成员变量的访问,synchronized就是为当前的线程声明一把锁

访问修饰符 synchronized 返回类型 方法名(参数列表){……}

public class Piao implements Runnable{
	int num =10;
	int count=0;
	boolean flag=true;
	
	@Override
	public void run() {
		while(!flag){
			sale();
		}
	}	
	public synchronized void sale() {		
			if(num<=0){
				flag=true;
				return;
			}
			num--;
			count++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+
					"抢到第"+count+"张票,现在还剩"+num+"张票");		
		
	}
}


public class Test {
	public static void main(String[] args) {
		Piao p=new Piao();
		Thread t1=new Thread(p,"张飒");
		Thread t2=new Thread(p,"赵四");
		Thread t3=new Thread(p,"王红");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

同步方法(2)

        使用synchronized关键字修饰的代码块

public void run() {
    while (true) {
        synchronized (this) {   //同步代码块
        // 省略修改数据的代码......
       // 省略显示信息的代码......
}}}
public class Piao implements Runnable {

	int num =10;
	int count=0;
	
	@Override
	public void run() {
		while(true){
			synchronized (this) {
				if(num<=0){
					break;
				}
				num--;
				count++;
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+
						"抢到第"+count+"张票,现在还剩"+num+"张票");			
			}
		}
	}
}


public class Test {

	public static void main(String[] args) {
		Piao p=new Piao();
		Thread t1=new Thread(p,"张飒");
		Thread t2=new Thread(p,"赵四");
		Thread t3=new Thread(p,"王红");
		
		t1.start();
		t2.start();
		t3.start();
	}
}

以上运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值