匿名内部类+线程预习- class Trim implements Runnable{
- public void run() {
- class Inner {
- public synchronized void sale() {
- int num = 100;
- while (num > 1)
- {
- num--;
- System.out.println(Thread.currentThread().getName()+"购买了"+num+"号票");
- }
- }
- }
- Inner in = new Inner();
- in.sale();
- }
- }
- class Demo1_Thread {
- public static void main(String[] args) {
- Trim t = new Trim();
- Thread t01 = new Thread(t);
- Thread t02 = new Thread(t);
- t01.start();
- t02.start();
- t01.setName("菜包狗");
- t02.setName("功夫兔");
- }
- }
复制代码 |