Package Com.subject01;public class threadorrunnable {public static void main (string[] args) {System.out.println (" Thread output: ");//Three threads for three different object resources, resources cannot be shared. Because it is not the same object, there is no thread-safe problem with new Threaddemo (). Start (); New Threaddemo (). Start (); New Threaddemo (). Start (); SYSTEM.OUT.PRINTLN ("Runnable Interface Implementation output:");//Three threads use the same object resource together to achieve resource sharing. However, there is also a thread insecurity problem, so object resources need to add synchronization settings Runnabledemo Rundemo = new Runnabledemo (); new Thread (Rundemo). Start (); New Thread ( Rundemo). Start (); new Thread (Rundemo). Start ();}} Class Threaddemo extends thread{private int ticket = 5; @Overridepublic void Run () {while (ticket>0) {System.out.println ("ticket=" +ticket--);}}} Class Runnabledemo implements runnable{private int ticket = 5; @Overridepublic void Run () {//thread insecure, need to add synchronization settings synchronized (this) {while (ticket>0) {try {thread.sleep (1000L);} catch (Interruptedexception e) {e.printstacktrace ();} System.err.println ("ticket=" +ticket--);}}}
http://blog.csdn.net/ns_code/article/details/17161237
Java Concurrency Implementation One (the difference between the thread and runnable of the concurrency implementation)