------<a href=
"http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
/*
需求:简单的卖票程序。
多个窗口卖票。
创建线程的第二种方式:实现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>、期待与您交流! -------