JAVA--多线程

一、多线程的书写步骤

1.线程类继承Thread

2.线程类中重写run()方法

3.测试类中创建线程对象,并用start()方法启动线程

Runnable接口的方式

1.线程类继承

2.创建线程对象:假设线程类是Runnable,则用Runnable myRunnable = new MyRunnable();创建对象,然后用Thread t = new Thread(myRunnable);创建

3.启动线程

public class Battle implements Runnable{
     
    private Hero h1;
    private Hero h2;
 
    public Battle(Hero h1, Hero h2){
        this.h1 = h1;
        this.h2 = h2;
    }
 
    public void run(){
        while(!h2.isDead()){
            h1.attackHero(h2);
        }
    }
}
        Battle battle1 = new Battle(gareen,teemo);
         
        new Thread(battle1).start();
 
        Battle battle2 = new Battle(bh,leesin);
        new Thread(battle2).start();

注意:这种方法必须创建一个对象

线程对象调用start(),方法和调用run()方法的区别

1.run()只有主线程一条执行路径

2.start():多条执行路径,主线程与子线程并行交替执行

匿名内部类的方法:直接在Test类中重写run方法

    Thread t1 = new Thread() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (!teemo.isDead()) {
                    gareen.attack(teemo);
                }
            }
        };
        t1.start();

 线程的一些常用方法

优先级有1~10表示,1最低,默认优先级为5

同步方法:

多线程的同步方法有3中:

1.匿名内部类中synchronize(一个对象)

                    synchronized (gareen) {
                        gareen.hurt();
                    }

2.方法中同步

    public void recover() {
        synchronized (this) {
            hp = hp + 1;
        }
    }

3.方法声明处同步

    public synchronized void  hurt() {
        hp = hp - 1;
    }

线程数组的使用

1.声明

        int n = 100;
        Thread[] addThread = new Thread[n];
        Thread[] reduceThread = new Thread[n];

2.创建每一个线程(匿名内部类的方式)

    for (int i = 0; i < n; i++) {
            Thread t1 = new Thread() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    synchronized (gareen) {
                        gareen.hurt();
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                    }
                }
            };
            t1.start();
            reduceThread[i] = t1;
        }

静态类的方法

 static class RecoverThread extends Thread{
        private Hero hero;
         
        public RecoverThread(Hero hero){
            this.hero = hero;
        }
         
        public void run(){
            while(true){
                hero.recover();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
         
    }

调用

        for (int i = 0; i < 2; i++) {
            new RecoverThread(gareen).start();
        }

3.for循环加入

        for (Thread t : reduceThread) {
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }

非线程安全的类转换为线程安全的类

 其他类似有:HashSet,LinkedList,HashMap等

        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = Collections.synchronizedList(list1);
        
原文地址:https://www.cnblogs.com/zxj-262410/p/8630627.html