1, Introduction. Processes and threads: Simply put, the process is responsible for opening up memory space for the program, and the thread is responsible for the specific execution unit (execution path).
There can be multiple execution paths in a process, called multithreading. The CPU can execute only one process at a time, but within a process there may be multiple threads executing.
The earliest multi-process, multi-threaded effect is actually the CPU to quickly switch the effect, because the switching rate quickly users can not find. It is now possible to use multiple CPUs.
Reference: http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
2. Thread state diagram
3, Thread creation: processes and threads are open by the system, Java interacts with the underlying, exposing the runnable interface and the thread class for the user to create threads.
the first way and considerations for thread creation
The first way to create this is to inherit the thread class, with two issues:
1, if this class has its own parent class will cause a conflict (of course, the parent class can inherit the thread class, but it is not recommended)
2. If you just want to use the method in the class as the thread's execution task and do not want to act as a thread object (because it will contain many thread-related useless methods)
At this point you can use the second way: Implement the Runnable interface, the specific process is shown below:
!!! The problem with multithreading is that
1, the use of data in the execution of the problem, is whether the data is the same (for example, the question of buying tickets, open multiple windows as a thread, but sold the same batch of votes)
2. Security issues using the same shared data across multiple threads
1 classSynlockdemo2 {3 Public Static voidMain (string[] args) {4Ticket t=NewTicket ();5Thread t1=NewThread (t);6Thread t2=NewThread (t);7 T1.start ();8 Try{Thread.Sleep (10);}Catch(Interruptedexception e) {}9t.flag=false;Ten T2.start (); One } A } - classTicketImplementsRunnable - { the Private intnum=100; - Booleanflag=true; - //Object Obj=new Object (); - Public voidrun () { + /* - about synchronization should be noted + 1, synchronization is modified by synchronized, but the lock is not a synchronous feature, because you want to add an object to use as a lock after A 2. The synchronous function does not declare a lock but has a synchronous effect, and the function naturally has an object this (in addition to the static function) at The following action is to encapsulate the same operation with a synchronous code block and a synchronization function, with a flag selected to enter two different blocks - If two threads are using the same lock, there is no security risk (here is the 0,-1,-2 ticket) - practice proves: normal When using this object, with security issues when using other objects - - because the static function loads with the class loading, there is no this, but a class file object is generated when the class is loaded into memory. - !! Using the This.getclass ()/class name. class, the difference between the two is that the former is a non-static method, and the latter is statically obtained with a class in - Note: A synchronization function can be thought of as a special case of synchronizing code blocks when using this to */ + if(flag) { - while(true){ the //synchronized (obj) { * synchronized( This){ $ if(num>0){Panax Notoginseng Try{Thread.Sleep (10);}Catch(Interruptedexception e) {} -System.out.println (Thread.CurrentThread (). GetName () + "... obj ..." +num--); the } + } A } the}Else{ + while(true){ - Show (); $ } $ } - } - Public synchronized voidShow () { the if(num>0){ -System.out.println (Thread.CurrentThread (). GetName () + "..." +num--);Wuyi } the } -}Synchronized Sync lock
1 classDeadlockdemo2 {3 Public Static voidMain (string[] args) {4Ticket t=NewTicket ();5Thread t1=NewThread (t);6Thread t2=NewThread (t);7 T1.start ();8 Try{Thread.Sleep (10);}Catch(Interruptedexception e) {}9t.flag=false;Ten T2.start (); One } A } - classTicketImplementsRunnable - { the Private intnum=100; - Booleanflag=true; -Object obj=NewObject (); - Public voidrun () { + /* - A deadlock occurs when there is more than one lock in a thread's execution code, and the lock is nested, when multiple threads hold different locks and do not do so, causing a deadlock problem + the performance of a deadlock is that the code runs abnormally. A */ at if(flag) { - while(true){ - synchronized(obj) { - Show (); - } - } in}Else{ - while(true){ to Show (); + } - } the } * Public synchronized voidShow () { $ synchronized(obj) {Panax Notoginseng if(num>0){ -System.out.println (Thread.CurrentThread (). GetName () + "..." +num--); the } + } A } the}dead Lock
Java Learning Note (javase Bi Xiangdong version video) four