JAVA线程使用指南

线程的创建,守护线程,线程等待,线程让出,线程同步,线程异步

同步块用的锁(synchronized 里面的参数)必须是同一个对象

package com.kaired.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Demo1 {

    public static void main(String[] args) throws InterruptedException {
        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();

        Thread thread_1 = new Thread(thread1);
        Thread thread_2 = new Thread(thread2);

        // 设置线程的优先级
        thread_1.setPriority(Thread.MIN_PRIORITY);
        thread_2.setPriority(Thread.MAX_PRIORITY);

        // 设置后台(守护)线程
        thread_1.setDaemon(true);

        // 启动1号线程
        thread_1.start();

        // 启动2号线程
        thread_2.start();

        // 设置线程等待
        thread_1.join();

        final Cars cars = new Cars();

        // 模拟线程同步错误, 使用同步方法解决
        Thread t_car = new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(cars.getCar());
                }
            }
        };

        Thread t_car2 = new Thread() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(cars.getCar());
                }
            }
        };

        t_car.start();
        t_car2.start();

        // 模拟线程同步错误,使用同步块解决
        Toilet toilet = new Toilet();
        Thread to_1 = new Thread() {
            @Override
            public void run() {
                toilet.up();
            }
        };
        Thread to_2 = new Thread() {
            @Override
            public void run() {
                toilet.up();
            }
        };
        to_1.start();
        to_2.start();

        System.out.println("结束");
    }
}

class Thread1 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }
    }
}

class Thread2 implements Runnable {
    @Override
    public void run() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < 1; i++) {
            try {
                Date time = Calendar.getInstance().getTime();
                System.out.println(sdf.format(time));
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

class Cars{
    int number = 10;

    // 当一个方法被synchronized修饰后,每次只能有一个线程进入该方法
    public synchronized int getCar() {
        if (number == 0) {
            throw new RuntimeException("无车了");
        }
        // 主动让出线程占用的CPU,此处是为了展现问题加上
        Thread.yield();
        return number--;
    }
}

class Toilet{

    public void up() {

        String name = Thread.currentThread().getName();

        try {
            System.out.println("可以同时干" + name);
            Thread.sleep(5000);

            synchronized (this) {
                System.out.println("不可以同时干" + name);
                Thread.sleep(5000);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        
        System.out.println("结束");

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 建立三个线程,并且同时运行它们。当运行时输出线程的名称。 实验步骤: (1)、创建类sy6_1 (2)、创建三个线程,调用start()方法启动这三个线程 (3)、保存文件,调试并编译运行程序。 参考程序运行效果: 2. 实现3个类:Storage、Counter和Printer。 Storage类应存储整数。 Counter应创建线程线程从0开始计数(0,1,2,3…)并将每个值存储到Storage类中。 Printer类应创建一个线程线程读取Storage类中的值并打印值。编写程序创建Storage类的实例,并创建一个Counter对象和Printer对象操作此实例。 实验步骤: (1)、创建三个类Counter, Printer,Storage (2)、创建TestCounter类,在该类中定义main函数,在main函数中定义Storage对象、Counter对象和 Printer对象,创建Counter线程和Printer线程并启动 (3)、保存文件,调试并编译运行程序。 参考程序运行效果: 3. 修改实验1第2题的程序,添加适当代码,以确保每个数字都恰好只被打印一次。 实验步骤: (1)、创建三个类Counter, Printer,Storage (2)、 创建TestCounter类,在该类中定义main函数,在main函数中定义Storage对象、Counter1对象和 Printer对象,创建Counter线程和Printer线程并启动 (3)、在定义Storage类中的setValue(int i) 和getValue ()方法时使用synchronized关键字,将其定义为同步方法 (4)、保存文件,调试并编译运行程序。 参考程序运行效果:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值