Threads in 23.java

Source: Internet
Author: User

    • Processes and Threads
      • Multithreading: Can (concurrently) run multiple tasks (programs) in the operating system
      • Multithreading: Multiple sequential streams (simultaneous) execution in the same application
      • Thread execution Process
            
      • Thread execution Process
            
      • Methods for creating threads
        • Method 1: Define a thread class, he inherits the class thread and overrides the method run (), the method run (), called the thread body; Because Java supports only single inheritance, classes defined in this way cannot inherit other classes
    • Multi-threaded program running mode and method
      • Defines a class Firstthread inherits the thread, and then, using a replication method, executes the code in run () when the thread executes
      • The Firstthread object is generated, and then the start () method of the object thread is called, and the thread enters the ready state, and the CPU gets into the execution state.
  1. classFirstThread extends Thread{
  2. publicvoid run(){
  3. for(int i =0; i <10; i++){
  4. System.out.println("FirstThread-->"+ i);
  5. }
  6. }
  7. }
  1. classTest{
  2. publicstaticvoid main(String arg []){
  3. //生成线程的对象
  4. FirstThread ft =newFirstThread();
  5. //启动线程
  6. //ft.run(); 千万不能这样写,这样就是单线程,会先执行run()中的方法
  7. ft.start();
  8. //线程交替运行,并且没有规律
  9. for(int i =0; i <10; i++){
  10. System.out.println("main-->"+ i);
  11. }
  12. }
  13. }
Results: D:\work\src>javac *.java D:\work\src>java testmain-->0firstthread-->0main-->1firstthread--> 1main-->2main-->3firstthread-->2firstthread-->3main-->4main-->5firstthread-->4main--> 6main-->7firstthread-->5firstthread-->6main-->8firstthread-->7main-->9firstthread--> 8firstthread-->9
    • Multithreaded Data security
      • Multi-threaded common data, there will be errors, need to introduce synchronous code block
    • Methods for synchronizing threads
      • Use: synchronized (this)
  1. classMyThread implements Runnable{
  2. int i =10;
  3. publicvoid run(){
  4. while(true){
  5. /*
  6. synchronized称为同步代码块;
  7. this代表调用run()方法的对象,称为同步锁;
  8. 功能:获得同步锁才有资格运行代码;
  9. **/
  10. synchronized(this){
  11. /*
  12. currentThread()是Thread的静态方法,获取当前代码在哪个线程中运行;
  13. Thread.currentThread().getName()可以获得线程的名字;
  14. **/
  15. System.out.println(Thread.currentThread().getName()+ i);
  16. i--;
  17. Thread.yield();
  18. if(i <0){
  19. break;
  20. }
  21. }
  22. }
  23. }
  24. }
  1. classTest{
  2. publicstaticvoid main(String arg []){
  3. MyThread myThread =newMyThread();
  4. //生成两个Thread对象,但是这两个Thread对象共用一个线程体
  5. Thread t1 =newThread(myThread);
  6. Thread t2 =newThread(myThread);
  7. /*每一个线程都有名字,可以通过Thread对象的setName()方法设置
  8. 也可以使用getName方法来获取线程的名字;**/
  9. t1.setName("线程a");
  10. t2.setName("线程b");
  11. //分别启动两个线程
  12. t1.start();
  13. t2.start();
  14. }
  15. }
The result is: D:\work\src>javac *.java d:\work\src>java test thread B10 thread B9 thread B8 thread b7 thread B6 thread b5 thread b4 thread b3 thread b2 thread b1 thread B0 thread A-1



From for notes (Wiz)



Threads in 23.java

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.