runable实现数据共享

第一课:认识下多线程

/**
* thread子类(并不是说Thread不能实现共享,因为Thread实现了runable接口,同样可以new Thread("继承Thread类的操作类").start(也可以实现共享))
*/

public class MyThread extends Thread{
	
	private int ticket = 12;

	@Override
	public  void run() {
		int num = 1;
		for (int i = 1; i <= 100; i++) {//for循环几次看做几个人买票(一张)
			if(ticket>0){
			System.out.println(Thread.currentThread().getName() + "剩余票数量为:"+--ticket+"执行了"+ ( num ++ ) +"次");
			}
		}
	}
	
	//一个类就是一个任务(售票),这个任务去派给n个人也就是n个窗口(线程)去执行,但这里演示的是各自线程去执行同一个任务,但数据却没有共享,这明显是不符合要求的
	public static void main(String[] args) {
		try {
			MyThread thA = new MyThread();
			MyThread thB = new MyThread();
			MyThread thC = new MyThread();
			thA.start();
			thB.start();
			thC.start();
			//thA.start();  throw new Exception("不能多次启用同一个线程")
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



/**
* runnable子类(避免单继承的尴尬)
*/
public class threadImplRunable implements Runnable{
	
	private int ticket = 12;

	@Override
	public  void run() {
		int num = 1;
		for (int i = 1; i <= 100; i++) {//for循环几次看做几个人买票(一张)
			if(ticket>0){
			System.out.println(Thread.currentThread().getName() + "剩余票数量为:"+--ticket+"执行了"+ ( num ++ ) +"次");
			}
		}
	}
	
	
	//一个类就是一个任务(售票),这个任务去派给n个人也就是n个窗口(线程)去执行,但这里演示的是各自线程去执行同一个任务,但数据却没有共享,这明显是不符合要求的
/**同时启动了3个线程对象,但与Thread操作不同的是,这三个线程都占用有个runnable接口对象的引用,所以实现了数据共享**/
	public static void main(String[] args) {
		try {
			threadImplRunable thA = new threadImplRunable ();
			new Thread(thA).start();
			new Thread(thA).start();
			new Thread(thA).start();
			//thA.start();不能多次启用同一个线程
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

第二课:线程的操作状态

1、创建-

2、就绪(建立新的线程对象后,调用start()方法就进入了就绪状态,进入线程队列排队,等待cpu调用分配)

3、运行(当就绪线程被调用并获得处理器资源是,就进入了运行状态,此时自动调用线程对象的run方法,也就是该线程的操作和功能)

4、堵塞(一个线程在某些特定情况下,如被人挂起或者需要耗时的输入输出操作时就会被让出cpu并终止自己的的执行,进入堵塞状态。比如调用了sleep,suspend,wait等就会处于堵塞状态,此时不能排入队列,只有当堵塞原因消除才能进入就绪状态)

5、终止状态(线程调用stop方法或run方法执行完成后就处于终止状态,处于其状态就不再具备继续运行的能力)

第三课:线程的优先级(public static final int ...)

MAX_PRIORITY------最高优先级

NORM_PRIORITY-----中等优先级

MIN_PRIORITY-------最低优先级

常用方法有三个:

public static final int MIN_PRIORITY()    最低优先级 1

publci void setPriority(int newPriority)     设置线程优先级(vip)

public final int getPriority();    获取线程优先级

举个例子:

public class MyThread implements Runnable{
	static int num = 0;
	@Override
	public void run() {
	for (int i = 0; i < 10; i++) {
/*		try {
			Thread.sleep(1000);//加不加都一样,都是先打印C
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
		System.out.println(Thread.currentThread().getName()+",循环的次数"+i);
	}

		
	}
	
	public static void main(String[] args) {
		MyThread mt = new MyThread();
		new Thread(mt,"线程A").start();//线程一启动
		new Thread(mt,"线程B").start();//线程二启动
		Thread th = new Thread(mt,"线程C");
		th.setPriority(Thread.MAX_PRIORITY);//设置为最高优先级
		th.start();//线程三启动
	}
	
}
线程C,循环的次数0
线程C,循环的次数1
线程C,循环的次数2
线程C,循环的次数3
线程C,循环的次数4
线程C,循环的次数5
线程C,循环的次数6
线程C,循环的次数7
线程C,循环的次数8
线程C,循环的次数9
线程A,循环的次数0
线程A,循环的次数1
线程A,循环的次数2
线程A,循环的次数3
线程B,循环的次数0
线程B,循环的次数1
线程B,循环的次数2
线程B,循环的次数3
线程B,循环的次数4
线程B,循环的次数5
线程B,循环的次数6
线程A,循环的次数4
线程B,循环的次数7
线程A,循环的次数5
线程A,循环的次数6
线程B,循环的次数8
线程A,循环的次数7
线程A,循环的次数8
线程B,循环的次数9
线程A,循环的次数9

//这里打印并不一定是C先开始,后续可在做研究????
抛转引玉:main方法也是个多线程,那么它的优先级又是多少呢?
public staitc void main(String[] args) throws Exception(){
    System.out.println(Thread.currentThread().getPriority());//5  中等优先级
}

猜你喜欢

转载自blog.csdn.net/qq_40826106/article/details/82663358
今日推荐