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

线程中途切换

在这里插入图片描述
在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);
            }
        }
    }
}
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值