#千锋逆战班 Java线程

.理想徘徊十字路口,不知道往哪边走;信心一路上低着头,数着脚下的石头

I.加入同步锁之后输出这两个字符串时无交互

public class Test8 {

	public static void main(String[] args) {
		Thread t1=new MyThread("aaa");
		Thread t2=new MyThread("bbb");
		t1.start();
		t2.start();
	}
}
class MyThread extends Thread{
	private String data;
	public MyThread(String data) {
		this.data=data;
	}
	public void run() {
		synchronized (data) {
			for (int i = 0; i < 100; i++) {
				System.out.println(data);
			}
		}
	}
}

II.线程综合

	public static void main(String[] args) {
		MyThread1 mt=new MyThread1();
		Thread tr=new Thread(mt);
		
		Thread t2=new MyThread2();
		tr.start();
		t2.start();
	}
}
class MyThread1 implements Runnable{
	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			try {
				Thread.sleep((int)(Math.random()*1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("hello"+"\t"+i);
		}
	}
}
class MyThread2 extends Thread{
	public void run() {
		for (int i = 0; i < 100; i++) {
			try {
				Thread.sleep((int)(Math.random()*1000));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("world"+"\t"+i);
		}
	}
}

III.

	public static void main(String[] args) {
		LeftChopstick left=new LeftChopstick();
		RightChopstick right=new RightChopstick();
		Thread boy=new Thread(new Boy(left,right));
		Thread girl=new Thread(new Girl(left,right));
		boy.start();
		girl.start();
	}
}
class LeftChopstick{
	String name="左筷子";
}
class RightChopstick{
	String name="右筷子";
}
class Boy implements Runnable{
	LeftChopstick left;
	RightChopstick right;
	public Boy(LeftChopstick left,RightChopstick right){
		this.left=left;
		this.right=right;
	}
	@Override
	public void run() {
		System.out.println("男孩要拿筷子!");
		synchronized (left) {
			try {
				left.wait();//高风亮节!把筷子资源让出去!
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("男孩拿到了左筷子,开始拿右筷子");
			synchronized (right) {//拿到右筷子,加锁!
				System.out.println("男孩拿到了右筷子,开始吃饭");
			}
		}
	}
}
class Girl implements Runnable{
	LeftChopstick left;
	RightChopstick right;
	public Girl(LeftChopstick left,RightChopstick right) {
		this.left=left;
		this.right=right;
	}
	@Override
	public void run() {
		System.out.println("女孩要拿筷子!");
		synchronized (right) {//女孩拿到右筷子资源,加锁!
			System.out.println("女孩拿到了右筷子,开始拿左筷子");
			synchronized (left) {//拿到左筷子资源,加锁!
				System.out.println("女孩拿到了左筷子,开始吃饭");
				left.notify();//女孩吃完后,唤醒等待左筷子锁得男孩线程
			}
		}
	}
}

输出参考:
男孩要拿筷子!
女孩要拿筷子!
女孩拿到了右筷子,开始拿左筷子
女孩拿到了左筷子,开始吃饭
男孩拿到了左筷子,开始拿右筷子
男孩拿到了右筷子,开始吃饭

V.

	public static void main(String[] args) {
		Shop shop=new Shop();
		
		Thread p=new Thread(new Product(shop),"生产者");
		Thread c=new Thread(new Customer(shop),"消费者");
		p.start();
		c.start();
	}
}
class Goods{
	private int id;
	public Goods(int id) {
		this.id=id;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
}
class Shop{
	Goods goods;
	boolean flag;//标识商品是否充足
	//生产者调用的 存的方法
	 public synchronized void saveGoods(Goods goods) throws InterruptedException {
		 //判断商品是否充足
		 if(flag==true) {
			 System.out.println("生产者:商品充足!要等待了!");
			 this.wait();//商品充足,生产者不用生产,而等待消费者买完!进入等待状态。 
		 }
		 //商品不充足!生产者生产商品,存在商场里
		 System.out.println(Thread.currentThread().getName()+"生产并在商场里存放了"+goods.getId()+"件商品");
		 this.goods=goods;
		 flag=true;//已经有商品了!可以让消费者购买了!
		 //消费者等待。。
		 this.notifyAll();//将等待队列的消费者唤醒!前来购买商品
	 }
	 //消费者调用的 取的方法
	 public synchronized void buyGoods() throws InterruptedException {
		 if(flag==false) {
			 System.out.println("消费者:商品不充足!要等待了!");
			 this.wait();
		 }
		 //正常购买商品
		 System.out.println(Thread.currentThread().getName()+"购买了"+goods.getId()+"件商品");
		 //商品买完了!标识没货了!
		 this.goods=null;
		 flag=false;
		 //唤醒生产者去生产商品
		 this.notifyAll();
	 }
}
//生产者
class Product implements Runnable{
	Shop shop;
	public Product(Shop shop) {
		this.shop=shop;
	}
	@Override
	public void run() {
		for (int i = 1; i <=5; i++) {
			try {
				this.shop.saveGoods(new Goods(i));
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
}
//消费者
class Customer implements Runnable{
	Shop shop;
	public Customer(Shop shop) {
		this.shop=shop;
	}
	@Override
	public void run() {
		for (int i = 1; i <=5; i++) {
			try {
				this.shop.buyGoods();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

输出参考:
消费者:商品不充足!要等待了!
生产者生产并在商场里存放了1件商品
生产者:商品充足!要等待了!
消费者购买了1件商品
消费者:商品不充足!要等待了!
生产者生产并在商场里存放了2件商品
生产者:商品充足!要等待了!
消费者购买了2件商品
消费者:商品不充足!要等待了!
生产者生产并在商场里存放了3件商品
生产者:商品充足!要等待了!
消费者购买了3件商品
消费者:商品不充足!要等待了!
生产者生产并在商场里存放了4件商品
生产者:商品充足!要等待了!
消费者购买了4件商品
消费者:商品不充足!要等待了!
生产者生产并在商场里存放了5件商品
消费者购买了5件商品

VI.高级线程基础

	public static void main(String[] args) {
		ExecutorService es= Executors.newFixedThreadPool(3);
		//ExecutorService es= Executors.newCachedThreadPool();
		MyTask task=new MyTask();
		es.submit(task);
		es.submit(task);
		es.submit(task);
		es.submit(task);
	
	}
}
class MyTask implements Runnable{
	public void run() {
		for (int i = 0; i <= 50; i++) {
			System.out.println(Thread.currentThread().getName()+":"+i);
			
		}
	}
}

VII.Future类

	public static void main(String[] args) throws InterruptedException, ExecutionException{
		ExecutorService es=Executors.newFixedThreadPool(3);
		MyCall mc=new MyCall();
		MyCall2 mc2=new MyCall2();
		//通过submit执行提交的任务,Future接受返回的结果
		Future<Integer> result=es.submit(mc);
		Future<Integer> result2=es.submit(mc2);
		//通过Future的get方法,获得线程执行完毕后的结果.
		Integer value=result.get();
		Integer value2=result2.get();
		System.out.println(value+value2);
	}
}
class MyCall implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		Integer sum=0;
		for(int i=1;i<=50;i++) {
			sum+=i;
		}
		return sum;
	}
}
class MyCall2 implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		Integer sum=0;
		for(int i=51;i<=100;i++) {
			sum+=i;
		}
		return sum;
	}
}

输出参考:
5050
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值