标签:c style class blog code java
package cn.felay.thread; class ThreadDemo1{ public void run(){ for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread()+""+i); } } } public class SingleThreadDemo { public static void main(String[] args) { new ThreadDemo1().run(); for (int i = 0; i <10; i++) { System.out.println(Thread.currentThread()+""+i); } } }
package cn.felay.thread; class ThreadDemo1 extends Thread{ public void run(){ for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread()+""+i); } } } public class SingleThreadDemo { public static void main(String[] args) { new ThreadDemo1().start(); for (int i = 0; i <10; i++) { System.out.println(Thread.currentThread()+""+i); } } }
package cn.felay.thread; class ThreadDemo1 implements Runnable{ public void run() { } } public class SingleThreadDemo { public static void main(String[] args) { new Thread(new ThreadDemo1(){ public void run(){ for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread()+""+i); } } }).start(); for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread()+""+i); } } }
package cn.felay.thread; public class ThreadDemo { public static void main(String[] args) { new Thread().run(); new Thread().run(); new Thread().run(); new Thread().run(); new Thread().run(); new Thread().run(); } }
package cn.felay.thread; public class ThreadDemo extends Thread{ public void run() { int i = 0; while(i<100){ System.out.println(Thread.currentThread().getName()+"==="+i); i++; } } public static void main(String[] args) { new ThreadDemo().run(); new ThreadDemo().run(); new ThreadDemo().run(); new ThreadDemo().run(); new ThreadDemo().run(); new ThreadDemo().run(); } }
package cn.felay.thread; public class ThreadDemo extends Thread{ public void run() { int i = 0; while(i<100){ System.out.println(Thread.currentThread().getName()+"==="+i); i++; } } public static void main(String[] args) { new ThreadDemo().start(); new ThreadDemo().start(); new ThreadDemo().start(); new ThreadDemo().start(); new ThreadDemo().start(); new ThreadDemo().start(); } }
package cn.felay.thread; public class ThreadDemo2 extends Thread { private int ticktes = 10; public void run() { while(ticktes>0){ System.out.println(Thread.currentThread().getName()+"出售了第"+(ticktes--)+"张票"); } } public static void main(String[] args) { new ThreadDemo2().start(); new ThreadDemo2().start(); new ThreadDemo2().start(); new ThreadDemo2().start(); } }
package cn.felay.thread; public class ThreadDemo2 extends Thread { private static int ticktes = 10; public void run() { while(ticktes>0){ System.out.println(Thread.currentThread().getName()+"出售了座位号为"+(ticktes--)+"的票"); } } public static void main(String[] args) { new ThreadDemo2().start(); new ThreadDemo2().start(); new ThreadDemo2().start(); new ThreadDemo2().start(); } }
package cn.felay.thread; public class ThreadDemo2 implements Runnable { private int ticktes = 10; public void run() { while(ticktes>0){ System.out.println(Thread.currentThread().getName()+"出售了座位号为"+(ticktes--)+"的票"); } } public static void main(String[] args) { ThreadDemo2 t =new ThreadDemo2(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); } }这样以来,我们即使不使用static修饰,我们依然能够进行同步操作,即所有的线程都共享的是同一个资源,不管这个资源多大,一旦使用线程销毁,这个资源也会随着销毁,因此它的开销只是在创建的时候才会有的.
黑马程序与----java线程学习,布布扣,bubuko.com
标签:c style class blog code java
原文地址:http://blog.csdn.net/u012332735/article/details/27720897