J2SE Basics: 13. Multithreaded programming

Source: Internet
Author: User

1: Processes and Threads




Process: Runs a program in which the program allocates the running space in memory. A process was started in memory.

Threads: Running in a process. Multiple threads can be switched between each other.

Main thread

Child Threads: Child threads are assigned from the main thread.


2: Importance of the main thread.




3: Creating Multithreading in Java




1: Inherit the Thread class:

2: Implement Runable Interface:

The difference between inheriting the thread class and implementing the Runable interface:

Java only supports single inheritance, and once a class inherits the thread class, it can no longer inherit other classes.




4:seleep method



5:thread.join method: If a thread joins another thread, the thread must wait for another thread


The Run method runs end (thread end). Method of adjusting = =






Thread.yield method: Give up the CPU resources, let the other threads execute.


Thread.setpriority (): Sets the priority of the thread. Priority, the longer the CPU is scheduled.
Max = 10
Normal = 5
Min = 0;
The value range is between 0---10.


Thread.seleep (): The Sleep of the thread. Unlike yield, it is dormant. Can be awakened.




Methods to stop a thread:

The old way of realizing it before

Stop/destory method. Causes deadlocks (to understand deadlocks in a way that combines iterators and collections)

Now the new way:


6: Why to use thread synchronization:


A: When multiple threads access a resource at the same time, it causes the resource data to be out of sync. Therefore, the resources are locked.



The lock-up process is that only one thread can access the resource when the threads are concurrent. So called



Synchronization of Threads.





7: The concept of the lock


8: What exactly locks:

Locks the object and class where the current method (current variable) resides.





Synchronized (synchronous)

Asynchronized (asynchronous)



9: How to Implement Thread synchronization:

A: Method Synchronization.



B: code block synchronization:



Method synchronization differs from code block synchronization:



Method synchronization: Locks the object on which the current method resides. Therefore, the entire method is executed until it is complete, locking the current object.



Code block synchronization: The lock on an object is only a block of code that executes the synchronized part. Other blocks of code will also appear

Current thread synchronization issues.





Synchronized (this) {

Count = count + 1;

try {

Thread.Sleep (2000);

} catch (Exception e) {

E.printstacktrace ();

}

System.out.println ("Current thread =" + Thread.CurrentThread (). GetName ()

+ ", count value =" + count);

}

System.out.println (count);



10: Coarse grain size of the lock:

The coarse granularity of the lock can only change the size of the lock in code block synchronization. There is no way to change the method's synchronization.



11: Collaboration between Threads:

Wait ();/notify ();



J2SE Basics: 13. Multithreaded programming

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.