关于java中的线程的笔记

Thread类和Runnable接口

这是两种用来创建线程的方法,但是这两种都要依赖于Thread类

  1. 第一种直接继承Thread类,然后通过实例化来产生线程对象
public class Test1 extends Thread {
	public Test1(String str) {
		super(str) ;   //线程的名字
	}
	public void run() {
	System.out.println(" Establishing a new thread: "+str);
	}
	public static void main(String[] args){
		Test1 temp = new Test1("Thread_test") ;
	}

2.通过实现Runnable接口,然后通过Thread类来创建线程

public class Test2 implements Runnable {
	private String name ;
	public Test2(String name) {
		System.out.println(" Establishing a new thread: "+name);
		this.name = name ;
	}
	
	public void run() {
		for(int i=0; i<5; i++) {
			System.out.println("Running thread: "+name);
		}
	}
	public static void main(String[] args){
		new Thread( new Test2("Runnable_test") ).start() ;	//这里使用的匿名类,所以在创建之后就直接启动线程
		}

从这以上的两个创建方法来看,线程的创建和Thread类是密不可分的

线程的状态

  1. 创建状态
  2. 可执行状态(就绪状态)
  3. 执行状态
  4. 不可执行状态(阻塞状态)
  5. 死亡状态

几种线程的方法

1.wait()
wait()方法无参时必须配合notify()方法使用,只有这样才能唤醒线程,使得线程继续运行
当wait() 方法含有参数时,可以被notify()方法唤醒,或者当超过参数的时间之后自动恢复
2.sleep(t)
使得线程休眠一段时间,让出cpu, 等待时间过后,自动恢复就绪状态
3.suspend()
使线程进入无限休眠状态,只有使用resume()方法才可以唤醒线程
4.yield()
让出本次执行的时间,之间进入就绪状态,等待下次的时间片

消费者—生产者模型

public class Test5 {
	public ReadWrite rw = new ReadWrite() ;
	public ShareInt shareInt = new ShareInt() ;
	
	public Test5() {
		ThreadReader test1 = new ThreadReader("thread1") ;
		ThreadReader test2 = new ThreadReader("thread2") ;
		ThreadReader test3 = new ThreadReader("thread3") ;
		ThreadWriter test4 = new ThreadWriter("thread4") ;
		ThreadWriter test5 = new ThreadWriter("thread5") ;
		ThreadWriter test6 = new ThreadWriter("thread6") ;
		
		test1.start(); 
		test2.start();
		test3.start();
		test4.start();
 		test5.start();
 		test6.start();
 		
	}
	
	public static void main(String[] args) {
		new Test5() ;
	}
	/*
	 * 内部类的使用
	 */
	
	//ThreadReader类
	class ThreadReader extends Thread{
		public ThreadReader(String name) {
			super(name);
		}
		
		public void run() {
			while(true) {
				rw.startReading();
				System.out.println(Thread.currentThread().getName()+" beging reading...");
				shareInt.getSharedInt();
				try {
					Thread.sleep((int)(Math.random()*1000));
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				
				System.out.println(Thread.currentThread().getName() + " end reading. ");
				rw.stopReading();
			}
		}
	}
	
	//ThreadWriter类
	class ThreadWriter extends Thread{
		public ThreadWriter(String name) {
			super(name);
		}
		
		public void run() {
			while(true) {
				rw.startWriting();
				System.out.println(Thread.currentThread().getName()+" beging writing... ") ;
				shareInt.setSharedInt(); 
				try {
					Thread.sleep((int)(Math.random()*1000));
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + " end writing. ");
				rw.stopWriting();
			}
		}
	}
	
	//ReadWrite类
	class ReadWrite{
		private int readerNumber = 0 ;  	//当前读者的数目
		private boolean writing = false ;	//标志当前是否可写
		private int waitingWriterNumber = 0 ;	//当前等待的写者的数目
		private boolean readerTurn = false ;	//标志当前是否可读
		
		synchronized public void startReading() {
										//如果正在进行写,或者可读标志为false进行等待
			while( writing || (waitingWriterNumber>0 && !readerTurn)) {
				try {
					wait() ;
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
			}
			++readerNumber ;
		}
		
		synchronized public void stopReading() {
			--readerNumber;				//当前读者的数目减一
			readerTurn = false ;
			notifyAll();				//唤醒可能等待的写线程
		}
		
		synchronized public void startWriting() {
			while( readerNumber>0 || writing ) {
				++waitingWriterNumber;	//进入等待的时候,使得等待写者的数目增加
				try {
					wait() ;
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				--waitingWriterNumber;	//在等待结束后,使得等待写者的数目减少
			}
			writing = true ;
		}
		
		synchronized public void stopWriting() {
			writing = false ;
			readerTurn = true ;			//如果存在读进程,执行读进程
			notifyAll() ;				//唤醒可能存在的读线程
			
		}
	}
	
	//公共资源类
	class ShareInt{
		private int shareInt = 1 ;
		public void setSharedInt() {
			shareInt++ ;
			System.out.println(Thread.currentThread().getName() + " setting shareInt to "+shareInt);
			
		}
		
		public int getSharedInt() {
			System.out.println(Thread.currentThread().getName() + " getting shareInt to " + shareInt);
			return shareInt ;
		}
	}
}

仔细研读一下这段代码,可以使得自己对于现成的理解更加深刻。
参考书籍《java2程序设计教程和上级实训》作者侯俊杰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

able陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值