Java_多线程

两种方法:

1.继承Thread类: newclass——Demo——选择java.lang.Thread(注意这里不是Demo1,且此方法不推荐,具有单继承的局限性,单继承的局限就是不能多继承,另外会使两个类的耦合性增加,如果父类有改动时会直接影响子类

package com.newcapec.thread;

public class Demo extends Thread {
	@Override
	public void run() {
		for(int i=0;i<10;i++) {
			//拿到当前线程的名称:Thread.currentThread().getName()
			System.out.println(Thread.currentThread().getName()+"-----i:-----"+i);
		}
	}
	public static void main(String[] args) {
		Demo demo1=new Demo();
		Demo demo2=new Demo();
		demo1.start();//将当前线程启动起来,启动后,若该线程获得CPU分配的资源,则自动会执行run方法内部的代码
		demo2.start();
	}
}

2.用一个类实现Runnable接口:newclass——Demo2——add——java.lang.Runnable(推荐用这个方法)

package com.newcapec.thread;

public class Demo2 implements Runnable{
	
	//用runnable接口实现的多线程
	
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+"-----i-----"+i);
			//拿到当前线程的名称
		}
	}
	
	public static void main(String[] args) {
		Demo2 demo2 = new Demo2();
		Thread th1 = new Thread(demo2,"线程1");
		Thread th2 = new Thread(demo2,"线程2");
		th1.start();
		th2.start();
	}

}

 


 多线程练习:爬山比赛:

1.Runnable: 

package com.newcapec.thread;

public class Climb1 implements Runnable {
	
	private int len=1500;//总距离
	@Override
	public void run() {
		int v=0;
		int sum=0;//爬了的距离
		int temp=0;//每100米
		if(Thread.currentThread().getName()=="年轻人") v=70;//年轻人速度
		if(Thread.currentThread().getName()=="老年人") v=30;//老年人速度
		while(true) {
			if(sum>=len) {
				System.out.println(Thread.currentThread().getName()+"爬到了终点!!!!!");
				break;
			}
			sum+=v;
			temp+=v;
			try {
				Thread.sleep(100);//时间间隔
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			//System.out.println(Thread.currentThread().getName()+"共爬了"+sum+"米");
			if(temp>=100) {
				System.out.println(Thread.currentThread().getName()+"又爬了100米");
				temp-=100;
			}
		}
	}
	public static void main(String[] args) {
		System.out.println("-------------开始爬山--------------");
		Climb1 r = new Climb1();
		Thread th1 = new Thread(r,"年轻人");
		Thread th2 = new Thread(r,"老年人");
		th1.start();
		th2.start();
	}
}
package com.newcapec.thread;

public class Climb1 implements Runnable {
	private int time;//爬100所需时间
	private int len=1000;//总距离
	private int num=0;//爬多少个100米
	@Override
	public void run() {
		if(Thread.currentThread().getName()=="年轻人") {
			time=500;
		}
		if(Thread.currentThread().getName()=="老年人") {
			time=1500;
		}
		synchronized (this) {
			num = len / 100;
			while (num > 0) {
				try {
					Thread.sleep(this.time);//模拟该人的爬完100米需要的事件
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName() + "爬了100米");
				num--;

			}
		}
		System.out.println(Thread.currentThread().getName()+"到达终点!!!!!");
	}
	public static void main(String[] args) {
		System.out.println("-------------开始爬山--------------");
		Climb1 r = new Climb1();
		Thread th1 = new Thread(r,"年轻人");
		Thread th2 = new Thread(r,"老年人");
		th1.start();
		th2.start();
	}
}

2.Thread:

package com.newcapec.thread;

public class Climb2 extends Thread {
	private int time;//爬100所需时间
	private int num=0;//爬多少个100米
	public Climb2(String name,int time,int len) {
		//name:用于当前线程名称的定义,len:山的高度
		super(name);//super:关键字在狗Z奥方法内部使用的意义是:调用父类对应参数的构造方法
		this.time=time;
		this.num=len/100;
	}
	@Override
	public void run() {
		while(num>0) {
			try {
				Thread.sleep(this.time);//模拟该人的爬完100米需要的事件
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"爬了100米");
			num--;
		}
		System.out.println(Thread.currentThread().getName()+"到达终点!!!!!");
	}
	public static void main(String[] args) {
		Climb2 young=new Climb2("年轻人",500,1000);
		Climb2 old=new Climb2("老年人",1500,1000);
		System.out.println("========开始爬山========");
		young.start();
		old.start();
	}
}

 


多线程练习:买票问题:

1.在run方法内部将核心代码放到同步锁代码块内

package com.newcapec.thread;

public class BuyTickets implements Runnable {

	private int count=50;//剩余票数
	private int num = 0;//第几张票
	@Override
	public void run() {
		while(true) {
			//不加锁会存在问题
			synchronized (this) {
				if(count<=0) {
					break;
					//跳出其外层循环
				}
				count--;
				num++;
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票,剩余"+count+"张票");
			}
		}
	}
	
	
	public static void main(String[] args) {
		System.out.println("-------------开始抢票--------------");
		BuyTickets r = new BuyTickets();
		Thread th1 = new Thread(r,"A");
		Thread th2 = new Thread(r,"B");
		Thread th3 = new Thread(r,"C");
		Thread th4 = new Thread(r,"D");
		th1.start();
		th2.start();
		th3.start();
		th4.start();
	}

}

2.将核心代码抽离放到另外方法内部,将该方法锁起来

package com.newcapec.thread;

public class BuyTickets implements Runnable {

	private int count=50;//剩余票数
	private int num=0;//第几张票
	private boolean flag=true;
	@Override
	public void run() {
		while(flag) {
			sale();
		}
	}
	
	public synchronized void sale() {
		if(count<=0) {
			flag=false;
		}else {
			count--;
			num++;
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"抢到第"+num+"张票,剩余"+count+"张票");
		}
	}

	public static void main(String[] args) {
		System.out.println("-------------开始抢票--------------");
		BuyTickets r = new BuyTickets();
		Thread th1 = new Thread(r,"A");
		Thread th2 = new Thread(r,"B");
		Thread th3 = new Thread(r,"C");
		Thread th4 = new Thread(r,"D");
		th1.start();
		th2.start();
		th3.start();
		th4.start();
	}
}

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我爱吃狮子头

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

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

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

打赏作者

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

抵扣说明:

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

余额充值