zoukankan      html  css  js  c++  java
  • java线程

    线程的实现:

    1.继承Thread 类,重写run函数

    package demo2;
    
    public class Demo1 {
        public static void main(String[] args) {
            Cat cat = new Cat();
            cat.start();
        }
    
    }
    
    class Cat extends Thread {
        int times = 0;
    
        // 重写run函数
        public void run() {
            while (true) {
                // 每一秒打印一次
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("hello,world");
                times++;
                if (times == 10)
                    break;
            }
        }
    }
    View Code

    2.实现Runable接口,重写run函数 

    package demo2;
    
    public class Demo1 {
        public static void main(String[] args) {
            Cat cat = new Cat();
            Thread t = new Thread(cat);
            t.start();
        }
    
    }
    
    class Cat implements Runnable {
    
        int times = 0;
    
        // 重写run函数
        public void run() {
            while (true) {
                // 每一秒打印一次
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("hello,world");
                times++;
                if (times == 10)
                    break;
            }
        }
    }
    View Code

    3.多线程运作

    package demo2;
    
    public class Demo2 {
        public static void main(String[] args) {
            Pig pig = new Pig(10);
            Bird bird = new Bird(10);
            Thread t1 = new Thread(pig);
            Thread t2 = new Thread(bird);
            t1.start();
            t2.start();
        }
    }
    
    class Bird implements Runnable {
        int n, ret = 0, times = 0;
    
        public Bird(int times) {
            this.times = times;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("飞出来一只鸟");
                ret++;
                if (times == ret)
                    break;
            }
        }
    }
    
    class Pig implements Runnable {
        int n, ret = 0, times = 0;
    
        public Pig(int times) {
            this.times = times;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                System.out.println("飞出来一只猫");
                ret++;
                if (times == ret)
                    break;
            }
        }
    }
    View Code

    深入理解:

    单线程运行

    package demo2;
    //import java.awt.Event;
    public class Demo1{
        
        public static void main(String[] args) {
            Ticket t=new Ticket();
            Thread p=new Thread(t);
            p.start();
        }
        
    }
    
    
    class Ticket implements Runnable{
        int num=1000;
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while(true){
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                num--;
                System.out.println("还剩下"+num);
            }
        }
        
        
    }
    View Code

    多线性运行:

    package demo2;
    
    //import java.awt.Event;
    public class Demo1 {
    
        public static void main(String[] args) {
            Ticket t = new Ticket();
            Ticket t1 = new Ticket();
            Ticket t2 = new Ticket();
            Thread p = new Thread(t);
            Thread p1 = new Thread(t1);
            Thread p2 = new Thread(t2);
            p.start();
            p1.start();
            p2.start();
    
        }
    
    }
    
    class Ticket implements Runnable {
        int num = 1000;
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                }
                num--;
                System.out.println("还剩下" + num);
            }
        }
    
    }
    View Code

    加上synchronized (this)  解决同步问题:

    synchronized (objiect)  其中object可以任意取,保证原子性

    (加锁)

    package demo2;
    
    //import java.awt.Event;
    public class Demo1 {
    
        public static void main(String[] args) {
            Ticket t = new Ticket();
            Thread p = new Thread(t);
            Thread p1 = new Thread(t);
            Thread p2 = new Thread(t);
            p.start();
            p1.start();
            p2.start();
    
        }
    
    }
    
    class Ticket implements Runnable {
        int num = 1000;
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                synchronized (this) {
                    if (num > 0) {
    
                        try {
                            Thread.sleep(100);
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                        num--;
                        System.out.println("还剩下" + num);
                    } else {
                        break;
                    }
                }
            }
        }
    
    }
    View Code
  • 相关阅读:
    Log4j学习
    HttpURLConnection请求
    正则表达式验证中文、图片上传
    freemarker学习
    参数中带有“&”符号问题
    禁止打印页面
    myEclipse 界面窗口打不开问题
    屏蔽网页右键
    分享功能
    table表格某一td内容太多导致样式混乱的解决方案
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10761943.html
Copyright © 2011-2022 走看看