Black Horse programmer _java08_ Multithreading

Source: Internet
Author: User
Tags thread class

Reproduced in: http://www.itxuexiwang.com/plus/view.php?aid=148

A thread is a task that can be executed in parallel in a program. The Java runtime always executes by selecting the highest-priority thread that is currently in the ready state. If several ready threads have the same priority, the time slice method will be used to take turns to allocate the processor. The process-to-thread difference process belongs to a separate running program, and the thread is a branch inside the program. Many threads make up the run of a program. Two ways to create a thread (1) The thread class is represented by the Threads class and its subclasses, and the inherited run () method defines the task body when the thread executes, defining a thread class that inherits from thread to override the run () method. (2) Any object that implements the interface runnable can be the target object of a thread, and the class thread itself implements the interface runnable, and the Run () method in the interface needs to implement the task body for the thread execution. (3) The thread's class method CurrentThread () can be used to obtain information about the current execution threads. Method One: public class person extends thread{public person (String name) {this.name=name;} @Override public void Run () {super. Run (); }} 1 2 3 4 5 6 7 8 9 Method Two: class Res {private string name; private string sex; Boolean flag=false; public synchronized Vo ID set (String name,string sex) {if (flag) {try{this.wait ()} catch (Exception e) {}} this.name=name; this.sex=sex; flag= True This.notify (); Public synchronized void out () {if (!flag) {try {this.wait ();} catch (Exception e) {} System.out.println ("Name=" +na Me+ "\ T" + "sex=" +sex); Flag=false; This.notify (); }}} class Input implements Runnable {private Res R; int x=0; Input (Res R) {this.r=r;public void Run () {if (true) {x==0 {r.set ("Mike", "Man"),} else {R.set ("Lili", "female"),} x= (x+1)%2;}} Class Output implements Runnable {private Res R; Output (Res R) {this.r=r;} public void Run () {while (true) {R.out ()}}} Class Threaddemo {public static void main (string[] args) {res r=new res (); new Thread (new Output (R)). Start (); new thread (New Input (R)). Start (); }} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 4 3 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 using the implementation runnable interface to define thread classes is more flexible in use than the thread class. At this point, the class implementing the interface runnable can inherit from the other parent class. If you simply rewrite the run () method instead of overriding the other thread methods, you should use the Runnable interface. Synchronization of Threads The Wait () method when the object executes the method, the thread that is accessing the object is blocked and temporarily stops running until another thread executes the Notify () and Notifyall () methods of the object until it wakes up. The wake-up thread should continue to test for the condition that caused the thread to be alerted. Each Java object has a lock, and each lock has only one key. Usually the object is not locked, and does not care about this matter. However, if the object has a synchronized method, the thread can only enter the thread if the key is acquired. This means that no other thread has entered the case before it can be entered. The deadlock of a thread is required to avoid some deadlocks during multithreaded access.Deadlocks are often caused by the fact that multiple threads share resources, while the lock order of shared resources is improper. There are two threads in the Daemon Java language: The daemon thread and the user thread. User threads are created in a thread. While the daemon thread has two sources, one is a thread created internally by the virtual machine, such as a garbage-collected thread, and the other is to create a thread-to-Setdeamon method that can invoke the thread class. Specifies that the thread is a daemon thread. The daemon thread serves the user thread, and the virtual machine exits when no user thread is running in the app. Class Stopthread implements Runnable {private Boolean flag=true; public synchronized void run () {while (flag) {try {WA It (); } catch (Interruptedexception e) {System.out.println (Thread.CurrentThread (). GetName () + "interruptedexception"); flag =false; } System.out.println (Thread.CurrentThread (). GetName () + "run Running"); }} public void Changeflag () {flag=false;}} Class Stopthreaddemo {public static void main (string[] args) {stopthread st=new stopthread (); Thread T=new Thread (ST); Thread T1=new Thread (ST); T.setdaemon (TRUE); T1.setdaemon (TRUE); T.start (); T1.start (); int num=0; while (true) {if (num++==70) {break;} System.out.println (Thread.CurrentThread (). GetName () + "Running ...". +num); }}} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 using synchronized this keyword modifier prevents two threads from entering the same method of an object at the same time. The object is considered to have multiple methods of synchronization, or a lock. Once a thread enters the synchronization method for that object, other threads cannot enter any of the synchronized threads on that object

Black Horse programmer _java08_ Multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.