Java 第17章 多线程基础 课堂练习+本章作业

发布时间:2023年12月27日

线程中途切换

在这里插入图片描述
在i == 5时开启创建的另一进程,并使用join使得其先执行完毕。

public class ThreadExercise {
    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        Thread thread = new Thread(t);
        for (int i = 1; i <= 10; i++) {
            Thread.sleep(1000);
            System.out.println("hi " + i);
            if (i == 5) {
                thread.start();
                thread.join();
            }
        }
        System.out.println("主线程结束");
    }
}

class T implements Runnable {
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println("hello " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("子线程结束");
    }
}

一个线程叫停另外一个线程

在这里插入图片描述
假设被叫停的类为A,叫停A的类为B。关键是要将A的对象作为B的内部成员,这样方便B对A的循环机制进行修改。

import java.util.Map;
import java.util.Scanner;

public class Homework01 {
    public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        a.start();
        b.start();
    }
}

class A extends Thread {
    private boolean loop = true;
    @Override
    public void run() {
        while (loop) {
            int num = (int)(Math.random() * 100 + 1);
            System.out.println(num);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("A exit..");
    }

    public void setLoop(boolean loop) {
        this.loop = loop;
    }
}

class B extends Thread {
    private A a;
    Scanner scn = new Scanner(System.in);
    public B(A a) {
        this.a = a;
    }
    @Override
    public void run() {
        while (true) {
            System.out.println("Please enter Q to exit: ");
            char ch = scn.next().toUpperCase().charAt(0);
            if (ch == 'Q') {
                a.setLoop(false);
                System.out.println("B exit..");
                break;
            } else {
                System.out.println("Your enter is not right..");
            }
        }
    }
}

线程同步问题

在这里插入图片描述
创建Card类实现Runnable 接口,为的是将来把Card类的对象传给两个Thread对象让它们共享这个Card对象,通过synchronized对Card类的锁来实现互斥。

public class Homework02 {
    public static void main(String[] args) {
        Card card = new Card();
        Thread thread1 = new Thread(card);
        Thread thread2 = new Thread(card);
        thread1.setName("A");
        thread2.setName("B");
        thread1.start();
        thread2.start();
    }
}
class Card implements Runnable {
    private int totalMoney = 10000;
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (totalMoney == 0) {
                    System.out.println("余额为0,不能再取了");
                    break;
                }
                totalMoney -= 1000;
                System.out.println(Thread.currentThread().getName() + " 取走1000,还剩 " + totalMoney);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
文章来源:https://blog.csdn.net/Winnie_deer/article/details/135242904
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。