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

来源:互联网 发布:蜂窝移动数据用量清零 编辑:程序博客网 时间:2024/06/04 21:00
------<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>、期待与您交流! -------
0 0
原创粉丝点击