A thorough understanding of Java multithreading-multithreading and thread synchronization

Source: Internet
Author: User

I. Multithreading
1. Fake Multithreading
Example 1:

  1. Public ClassTestThread
  2. {
  3. IntI = 0, j = 0;
  4. Public VoidGo (IntFlag ){
  5. While(True){
  6. Try{
  7. Thread. Sleep (100 );
  8. }
  9. Catch(InterruptedExceptionE ){
  10. System. Out. println ("Interrupted ");
  11. }
  12. If(Flag = 0)
  13. I ++;
  14. System. Out. println ("I =" + I );
  15. }
  16. Else{
  17. J ++;
  18. System. Out. println ("j =" + j );
  19. }
  20. }
  21. }
  22. Public Static VoidMain (String[] Args ){
  23. NewTestThread (). go (0 );
  24. NewTestThread (). go (1 );
  25. }
  26. }

The running result of the above program is:
I = 1
I = 2
I = 3
...
The result will always print the I value. Our intention is that when sleep () is called in the while LOOP, another thread will start and print the value of j, but the result is not like this. The following describes why sleep () does not show the expected results.
2. Multithreading
By inheriting the class Thread or implementing the Runnable interface, we can implement multithreading.
2.1 multi-Thread implementation by inheriting class Thread
Class Thread has two most important functions: run () and start ().
1) The run () function must be overwritten. Put the code to be processed in parallel in multiple threads into this function.
2) Although the run () function implements parallel processing of multiple threads, we cannot directly call the run () function, but call the start () function to call the run () function. When you call start (), the start () function first initializes the multi-thread (this is why the run () function cannot be called directly ), then call the run () function.
Example 2:
  1. Public ClassTestThreadExtends Thread{
  2. Private Static IntThreadCount = 0;
  3. Private IntThreadNum = ++ threadCount;
  4. Private IntI = 5;
  5. Public VoidRun (){
  6. While(True){
  7. Try{
  8. Thread. Sleep (100 );
  9. }
  10. Catch(InterruptedExceptionE ){
  11. System. Out. println ("Interrupted ");
  12. }
  13. System. Out. println ("Thread" + threadNum + "=" + I );
  14. If(-- I = 0)Return;
  15. }
  16. }
  17. Public Static VoidMain (String[] Args ){
  18. For(IntI = 0; I <5; I ++)
  19. NewTestThread (). start ();
  20. }
  21. }

The running result is:
T

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.