java基础01——线程

一个进程中需要多个控制流程时,就用到了线程。进程运行时开辟了一块地址空间,一个进程下的多个线程共享这块地址空间,可以数据共享,互相通讯。

一、创建线程的两种方式
实现Runnable接口

public class MyRunnable implements Runnable {

	@Override
	public void run() {
		for(int i = 0;i<100;i++) {
			System.out.println(Thread.currentThread().getName()+"i:"+i);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

继承Thread类

public class MyThread extends Thread{
	public MyThread(String name) {
		super(name);
	}
	public void run() {
		for(int i = 0;i<100;i++) {
			System.out.println(Thread.currentThread().getName()+"i:"+i);
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

测试

public class ThreadDemo {
	public static void main(String[] args) {
		MyThread thread1 = new MyThread("线程一");
		MyThread thread2 = new MyThread("线程二");
		MyThread thread3  = new MyThread("线程三");
		thread1.start();
		thread2.start();
		thread3.start();
		MyRunnable myRunnable  = new MyRunnable();
		Thread thread4 = new Thread(myRunnable,"线程4");
		thread4.start();

	}

}

二、锁的理解,这里以生产者消费者举例。生产者往仓库里加商品,消费者从仓库里取商品。这里的仓库就是锁。
仓库:

package fuxi01_thread;

import java.util.ArrayList;
import java.util.List;

public class CangKu {
	private List<String> cangku = new ArrayList<String>();
	private  int size = 20;
	
	public String remove() {
		String s = "";
		if(cangku.size() > 0) {
			s = cangku.remove(0);
		}
		return s;
	}
	
	public boolean add(String product) {
		boolean flg;
		if(cangku.size() > size) {
			flg = false;
		}else {
			flg = cangku.add(product);
		}
		System.out.println("仓库中商品数量:"+cangku.size());
		return flg;
	}
}

生产者:

package fuxi01_thread;

import java.util.Random;

public class ShengChanZhe implements Runnable {
	private CangKu cangku;
	public  ShengChanZhe(CangKu cangku) {
		this.cangku = cangku;
	}
	@Override
	public void run() {
		while(true) {
			synchronized (cangku) {
				String product = "商品:"+new Random().nextInt(100);
				boolean flg= cangku.add(product);
				if(flg) {
					System.out.println(Thread.currentThread().getName()+"添加商品:"+product);
				}
				try {
					cangku.notifyAll();
					Thread.sleep(400);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}

消费者:

package fuxi01_thread;

import java.util.Random;

public class XiaoFeiZhe implements Runnable {
	private CangKu cangku;
	public  XiaoFeiZhe(CangKu cangku) {
		this.cangku = cangku;
	}
	@Override
	public void run() {
		while(true) {
			synchronized (cangku) {
				String s = cangku.remove();
				if(!"".equals(s)) {
					System.out.println(Thread.currentThread().getName()+"买走了商品:"+s);
				}else {
					try {
						cangku.wait();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}		
	}
}

测试类:

package fuxi01_thread;

public class Test {
	public static void main(String[] args) {
		CangKu cangKu = new CangKu();
		
		ShengChanZhe shengChanZhe1 = new ShengChanZhe(cangKu);
		ShengChanZhe shengChanZhe2 = new ShengChanZhe(cangKu);
		
		XiaoFeiZhe xiaoFeiZhe1 = new XiaoFeiZhe(cangKu);
		XiaoFeiZhe xiaoFeiZhe2 = new XiaoFeiZhe(cangKu);
		XiaoFeiZhe xiaoFeiZhe3 = new XiaoFeiZhe(cangKu);
		
		new Thread(shengChanZhe1,"生产者1").start();
		new Thread(shengChanZhe2,"生产者2").start();
		new Thread(xiaoFeiZhe1,"消费者1").start();
		new Thread(xiaoFeiZhe2,"消费者2").start();
		new Thread(xiaoFeiZhe3,"消费者3").start();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值