zoukankan      html  css  js  c++  java
  • Java如何实现多线程

    JAVA里面
    
    ​	JVM(java虚拟机) 就是 一个进程
    
    ​	进程与进程之间是绝对互相独立
    
    ​	我们运行多个main方法,代表有多个JAVA进程
    
    ​	进程里面有线程;  一个进程里面,允许有多个线程叫多线程;

    代码顺序分先后,线程的执行自己跑自己的

      1:继承Thread

      

     1 public class MyThread extends Thread{
     2     
     3     public void run(){
     4         
     5         //我们这个线程需要实现的功能;要完成任务
     6         
     7     }
     8 }
     9 
    10 //1: 新建一个线程对象;
    11 //2: 调用start方法;

      

     1 package com.lv.study.pm.second;
     2 
     3 public class Test1 {
     4 
     5     public static void main(String[] args) {
     6 
     7         System.out.println("1:");
     8         
     9         Thread th1=new MyTh();
    10         Thread th2=new MyTh();        
    11         
    12         //没有启动线程  只是在主线程里面调用我们的run方法
    13         //th1.run();
    14         //th2.run();
    15         
    16         //启动线程
    17         th1.start();
    18         th2.start();
    19         
    20         //线程优先级
    21         th2.setPriority(Thread.MAX_PRIORITY);
    22         th1.setPriority(Thread.MIN_PRIORITY);
    23         
    24         System.out.println("2:");
    25         
    26     }
    27 
    28 }
    29 
    30 
    31 class MyTh extends Thread{
    32     
    33     public void run(){
    34         
    35         //线程的礼让
    36         Thread.yield();
    37         
    38         for (int i = 0; i < 10; i++) {
    39             
    40             System.out.println(this.getName()+""+i);
    41         }
    42     }
    43 }

      2:实现Runnable

      

     1 public class MyRunable implements Runnable{
     2     
     3     public void run(){
     4         
     5         //做我们要做的任务;
     6         
     7     }
     8     
     9 }
    10 
    11 //1:新建一个MyRunable对象;
    12 //2:新建一个Thread对象(MyRunable);
    13 //3:通过trhead对象来调用start方法;运行我们的线程;
     1 package com.lv.study.pm.first;
     2 
     3 public class Test2 {
     4 
     5     public static void main(String[] args) {
     6 
     7          //java实现多线程
     8         //1:标准  Runable接口
     9         
    10         //接口的实现着
    11         Runnable r=new MyRun();
    12         
    13         
    14         //2:接口的使用者  这个使用者没有实现着代表啥事情也没有干
    15         Thread th=new Thread(r);
    16         th.start();//启动了线程
    17         
    18     }
    19 
    20 }
    21 
    22 
    23 //Runnable接口的实现着
    24 class MyRun implements Runnable{
    25 
    26     @Override
    27     public void run() {
    28 
    29         for (int i = 0; i < 10; i++) {
    30             
    31             System.out.println(i);
    32         }
    33         
    34     }
    35     
    36 }

     

  • 相关阅读:
    p5js弹钢琴
    javascript——拖拽(完整兼容代码)
    js正则表达式和replace
    CSS最常用和实用的技巧
    优化MYSQL数据库的方法
    css默认样式
    javascript之document对象
    常见JS(JavaScript)冲突解决方法
    String对象中的正则表达式
    css去掉a标签点击后的虚线框
  • 原文地址:https://www.cnblogs.com/dabu/p/12465557.html
Copyright © 2011-2022 走看看