Java:1.多线程的3种创建方式;2.守护线程;3.龟兔赛跑问题;4.3个售票员同时卖票问题;5.线程死锁与线程协调。

1.多线程的3种创建方式

(1)通过继承Thread类创建

public class ThreadDemo extends Thread {
    
    
    @Override
    public void run() {
    
    
        System.out.println("子线程");
    }

    public static void main(String[] args) {
    
    
        ThreadDemo threadDemo = new ThreadDemo();
        threadDemo.start();
    }
}

(2)通过Runnable接口创建

public class ThreadRunnable implements Runnable {
    
    
    @Override
    public void run() {
    
    
        System.out.println("子线程");
    }

    public static void main(String[] args) {
    
    
        ThreadRunnable threadRunnable = new ThreadRunnable();
        Thread thread = new Thread(threadRunnable);
        thread.start();
    }
}

(3)通过Callable接口实现

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadCallable implements Callable {
    
    
    @Override
    public Object call() throws Exception {
    
    
        System.out.println("子线程");
        return "test";
    }

    public static void main(String[] args) {
    
    
        ThreadCallable threadCallable = new ThreadCallable();
        FutureTask futureTask = new FutureTask(threadCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
    
    
            System.out.println("子线程返回值为:" + futureTask.get());
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        } catch (ExecutionException e) {
    
    
            e.printStackTrace();
        }
    }
}

2.守护线程

public class ThreadDaemon {
    
    
    public static void main(String[] args) {
    
    
        //用户线程
        User user = new User();
        Thread thread1 = new Thread(user);
        thread1.start();
        //守护线程
        Daemon daemon = new Daemon();
        Thread thread2 = new Thread(daemon);
        thread2.setDaemon(true);
        thread2.start();

    }
}

class User implements Runnable {
    
    

    @Override
    public void run() {
    
    
        for (int i = 1; i <= 100; i++) {
    
    
            System.out.println("用户线程" + i);
        }
    }
}

class Daemon implements Runnable {
    
    

    @Override
    public void run() {
    
    
        int i = 1;
        while (true) {
    
    
            System.out.println("守护线程" + i++);
        }
    }
}

3.龟兔赛跑问题

public class Race implements Runnable {
    
    
    private static String winner = null;

    @Override
    public void run() {
    
    
        for (int i = 1; i <= 100; i++) {
    
    
            boolean flag = gameOver(i);
            System.out.println(Thread.currentThread().getName() + "跑了" + i + "米");
            if (flag == true) {
    
    
                break;
            }
            //模拟兔子睡觉
            if ("兔子".equals(Thread.currentThread().getName()) && i == 50) {
    
    
                try {
    
    
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

    public boolean gameOver(int meters) {
    
    
        //看谁先跑完100米
        if (meters == 100) {
    
    
            winner = Thread.currentThread().getName();
            System.out.println("冠军是" + winner);
            return true;
        }
        /*
        分出胜负,目的是告诉另一个线程未跑完100米的进程该终止了,若不加此判断语句,
        两个进程,即乌龟和兔子都会跑完100米
         */
        if (winner != null) {
    
    
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
    
    
        Race race = new Race();
        Thread thread1 = new Thread(race, "兔子");
        Thread thread2 = new Thread(race, "乌龟");
        thread1.start();
        thread2.start();
    }
}

4.3个售票员同时卖票问题

public class SellTicket implements Runnable {
    
    
    public static int ticket = 100;

    @Override
    public void run() {
    
    
        while (ticket > 0) {
    
    
            try {
    
    
                Thread.sleep(100);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            //同步语句
            synchronized ("") {
    
    
                if (ticket <= 0) {
    
    
                    return;
                }
            }
            System.out.println(Thread.currentThread().getName() + "卖了1张票,还剩" + --ticket + "张票");
        }
    }

    public static void main(String[] args) {
    
    
        SellTicket sellTicket = new SellTicket();
        Thread thread1 = new Thread(sellTicket, "售票员1");
        Thread thread2 = new Thread(sellTicket, "售票员2");
        Thread thread3 = new Thread(sellTicket, "售票员3");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

5.线程死锁与线程协调

(1)线程死锁

public class DeadLock {
    
    
    public static void main(String[] args) {
    
    
        DeadLockA deadLockA = new DeadLockA();
        Thread thread1 = new Thread(deadLockA);

        DeadLockB deadLockB = new DeadLockB();
        Thread thread2 = new Thread(deadLockB);

        thread1.start();
        thread2.start();

    }
}


class DeadLockA implements Runnable {
    
    

    @Override
    public void run() {
    
    
        synchronized ("A") {
    
    
            System.out.println("进程1的A锁");
            synchronized ("B") {
    
    
                System.out.println("进程1的A锁和B锁");
            }
        }
    }
}

(2)线程协调

public class DeadLock {
    
    
    public static void main(String[] args) {
    
    
        DeadLockA deadLockA = new DeadLockA();
        Thread thread1 = new Thread(deadLockA);

        DeadLockB deadLockB = new DeadLockB();
        Thread thread2 = new Thread(deadLockB);

        thread1.start();
        thread2.start();

    }
}


class DeadLockA implements Runnable {
    
    

    @Override
    public void run() {
    
    
        synchronized ("A") {
    
    
            System.out.println("进程1的A锁");
            try {
    
    
                "A".wait();
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            synchronized ("B") {
    
    
                System.out.println("进程1的A锁和B锁");
            }
        }
    }
}

class DeadLockB implements Runnable {
    
    

    @Override
    public void run() {
    
    
        synchronized ("B") {
    
    
            System.out.println("进程2的B锁");
            synchronized ("A") {
    
    
                System.out.println("进程2的B锁和A锁");
                "A".notify();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45631296/article/details/103266425