zoukankan      html  css  js  c++  java
  • java学习之线程的操作方法

     1 package com.gh.thread;
     2 
     3 /**
     4  * 线程实现的两种方法 
     5  * 1、继承thread类 
     6  * 2、实现Runnable接口
     7  * @author ganhang
     8  * 
     9  */
    10 public class ThreadDemo {
    11         
    12     public static void main(String[] args) {
    13         //第一种线程实现
    14         Mythread mythread =new Mythread();
    15         mythread.setName("自定义线程1");
    16         mythread.start();
    17         try {
    18             Thread.sleep(2);//当前线程睡眠2毫秒
    19         } catch (InterruptedException e) {
    20             e.printStackTrace();
    21         }
    22         System.out.println(Thread.currentThread().getName());
    23         //第二种线程实现
    24         Thread thread=new Thread(new Mythread2(),"自定义线程2");
    25         thread.start();
    26     }
    27 }
    28 
    29 class Mythread extends Thread{
    30     @Override
    31     public void run() {
    32         for (int i = 0; i < 3; i++) {
    33             //输出当前线程的名字和i;
    34             System.out.println(Thread.currentThread().getName()+"--"+i);
    35         }
    36     }
    37 }
    38 class Mythread2 implements Runnable{
    39 
    40     @Override
    41     public void run() {
    42         for (int i = 0; i <3; i++) {
    43             System.out.println(Thread.currentThread().getName()+"--"+i);
    44         }
    45     }
    46     
    47 }

    线程的同步

     1 package com.gh.thread;
     2 /**
     3  * 同步实现顺序上厕所,假设厕所只有一个
     4  * 1、使用synchronized同步代码块
     5  * 2、使用同步方法
     6  * @author ganhang
     7  */
     8 public class ThreadDemo1 {
     9     public static void main(String[] args) {
    10         Mythread5 my=new Mythread5();
    11         //注意这里必须使用同一个Mythread类的对象,不然无法同步
    12         Thread t1=new Thread(my,"小白");//线程1
    13         Thread t2=new Thread(my,"小黑");//线程2
    14         //两个线程同时跑
    15         t1.start();//小白要上厕所
    16         t2.start();//小黑也要上厕所
    17     }
    18 }
    19 class Mythread5 implements Runnable{
    20     @Override
    21     public void run() {
    22         //同步代码块
    23         synchronized (this) {//锁定对象一次只能进一个个
    24         System.out.println(Thread.currentThread().getName()+"正在上厕所。。");
    25         try {
    26             Thread.sleep(1000);//模拟**。。。耗时1s
    27         } catch (InterruptedException e) {
    28             e.printStackTrace();
    29         }
    30         System.out.println(Thread.currentThread().getName()+"上完了。。");
    31         }
    32     }
    33 }

    中断线程

     1 package com.gh.thread;
     2 
     3 /**
     4  * 中断线程 
     5  * 1、interrupt()方法 
     6  * 2、自定义标记完成中断
     7  * @author ganhang
     8  * 
     9  */
    10 public class ThreadDemo2 {
    11     public static void demo1() {
    12         Thread t1 = new Thread(new Mythread3(), "线程1");
    13         t1.start();
    14         for (int i = 0; i < 10; i++) {
    15             if (i == 5)
    16                 t1.interrupt();//只是个标记中断不会真正中断,是否中断还是由线程自己决定
    17             System.out.println("main-" + i);
    18             try {
    19                 Thread.sleep(1000);
    20             } catch (InterruptedException e) {
    21                 e.printStackTrace();
    22             }
    23         }
    24     }
    25     public static void demo2(){
    26         Mythread4 my=new Mythread4();
    27         Thread t1=new Thread(my,"线程2");
    28         t1.start();
    29         for(int i=0;i<10;i++){
    30             if(i==5){
    31                 my.setFlag(true);//中断
    32             }
    33             System.out.println(Thread.currentThread().getName()+"--"+i);
    34             try {
    35                 Thread.sleep(1000);
    36             } catch (InterruptedException e) {
    37                 e.printStackTrace();
    38             }
    39         }
    40     }
    41     public static void main(String[] args) {
    42         demo1();//第一种方式实现
    43         demo2();//第二种方式实现
    44     }
    45 }
    46 
    47 class Mythread4 implements Runnable {
    48     private boolean flag = false;
    49 
    50     public boolean isFlag() {
    51         return flag;
    52     }
    53 
    54     public void setFlag(boolean flag) {
    55         this.flag = flag;
    56     }
    57 
    58     @Override
    59     public void run() {
    60         int i=0;
    61         while (!flag) {
    62                 System.out.println(Thread.currentThread().getName() + "--" + i++);
    63                 try {
    64                     Thread.sleep(1000);
    65                 } catch (InterruptedException e) {
    66                     e.printStackTrace();
    67                 }
    68             
    69         }
    70     }
    71 }
    72 
    73 class Mythread3 implements Runnable {
    74 
    75     public void run() {
    76         int i = 0;
    77         while (!Thread.interrupted()) {
    78             System.out.println(Thread.currentThread().getName() + "--" + i++);
    79             try {
    80                 Thread.sleep(1000);// sleep会清除interrupt状态
    81             } catch (InterruptedException e) {
    82                 e.printStackTrace();
    83                 Thread.currentThread().interrupt();
    84             }
    85         }
    86     }
    87 }
  • 相关阅读:
    最值
    算数平均值/加权平均值
    jdbatemplate使用
    所有正规jar包
    java代码在计算机中经历的三个阶段,反射的原理
    lambda函数式编程思想
    重写equals方法
    安全牛-弱点扫描
    安全牛-主动收集
    安全牛-被动收集
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154356.html
Copyright © 2011-2022 走看看