黑马程序员_java基础自学经典程序记录

------<a href=
/*
需求:简单的卖票程序。
多个窗口卖票。
创建线程的第二种方式:实现Runnable接口
步骤:
1,定义类事项Runnable接口
2,覆盖Runnable接口中的run方法
3,通过Thread类建立线程对象
4,将Runnable接口的子类对象传递给Thread的构造函数
5,调用Thread类的start方法开启线程并调用Runnable接口子类的run方法

实现方式和继承方式区别:
实现方式避免了单继承的局限性。
继承Thread:线程代码存放Thread子类run方法中。
实现Runnable,线程代码存在接口的子类run方法中。
*/

class Ticket implements Runnable// extends Thread 
{
	private int tick = 100;
	public void run()
	{
		while(true)
		{
			if(tick>0)
			{
				System.out.println(Thread.currentThread().getName()+"...sale:"+tick--);
			}
			
		}
	}
	
}

class ThreadDemo
{
	public static void main(String[] args)
	{
		Ticket t = new Ticket();

		Thread t1 = new Thread(t);
		Thread t2 = new Thread(t);
		Thread t3 = new Thread(t);
		Thread t4 = new Thread(t);
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	
	}
}
/*答案:134 13423;考点:Exception执行顺序*/
class Test 
{
	public static String output="";
	public static void foo(int i)
	{
		try
		{
			if(i==1)
				throw new Exception();
			output+="1";
		}
		catch (Exception e)
		{
			output+="2";
			return;
		}
		finally
		{
			output+="3";
		
		}
		output+="4";
	}
	public static void main (String args[])
	{
		foo(0);
		System.out.pringln(output);
		foo(1);
		System.out.println(output);
	
	}
}

"http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值