黑马程序员----多线程基础

二次信任 提交于 2020-03-12 05:15:53

/**
* @author Administrator
*
* @description Java多线程编程入门测试类
* @history
*/
// 方法一、继承线程类Thread
class MyThread extends Thread{
public MyThread(String threadName){ // 设置当前线程的名称
currentThread().setName(threadName);
}
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
// 方法二、实现Runnable接口
class MyThread2 implements Runnable{
public void run() {

System.out.println(Thread.currentThread().getName());
}
}
public class SynTestDemo {
/**
*@description
*@param args
*/
public static void main(String[] args) {
// 1、线程的定义、线程和进程的区别、为什么要引入线程等
// 2、 Java实现多线程的方法主要有两种:继承Thread类和实现Runnable接口
// 3、多个线程并发同时访问公共的临界资源的时候需要进行同步处理,过多的同步不当会造成死锁
问题
MyThread t1 = new MyThread("hello-thread");
t1.start(); // 启动线程1
MyThread2 t2 = new MyThread2();
new Thread(t2).start(); // 启动线程2
}
}
/**
* @author Administrator
*
* @description 多线程编程演练例子
* @history
*/
public class SynABCTest {
/**
* @description
* @param args
*/
public static void main(String[] args) {
// 通过具体的例子来加深对多线程的认识
// 问题为:循环打印10遍ABCABC...ABC
PrintThread p1 = new PrintThread(0, "A"); // 参数为线程标志和打印的内容
PrintThread p2 = new PrintThread(1, "B");
PrintThread p3 = new PrintThread(2, "C");
// 启动线程A B C
new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
}

}
// 采用实现接口的方式定义线程类
class PrintThread implements Runnable {
// 标记执行当前应该执行的线程0、 1、 2依次表示A B C
// 定义成静态变量,因为线程各自使用独立的栈
private static int index = 0;
private static Object lock = new Object();
private int key = 0; // 线程标志
private int print = 0; // 打印的次数
private String name; // 打印的内容
public PrintThread(int key, String name) {
this.key = key;
this.name = name;
}
public void run() {
while (this.print < 10) { // 打印的次数
synchronized (lock) {
while (!(this.key == index % 3)) { // 从0开始执行
try {
lock.wait(); // 阻塞掉
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(this.name); // 打印出内容
this.print++; // 当前线程打印次数++
index++; // 线程切换下一个
lock.notifyAll(); // 唤醒其他等待的线程
}
}
}
}
/**
* @author Administrator
*
* @description 死锁模拟测试类
* @history
*/
public class DeadLockTest {
/**
*@description
*@param args*/
public static void main(String[] args) {
// 过多的同步操作可能会造成死锁问题,死锁产生的原因是形成了环路等待
// 通过两个线程对象进行模拟,线程A完成一个操作需要资源1和资源2,线程B也是一样
// 在资源分配的过程中,线程A占用了资源1,等待资源2,此时此刻线程B占用了资源2,等待资源1
DeadThread dt1 = new DeadThread("1", true);
DeadThread dt2 = new DeadThread("2", false);
new Thread(dt1).start(); // 启动线程1
new Thread(dt2).start(); // 启动线程2
}
// 定义静态内部类、类似外部类了
static class DeadThread implements Runnable{
/**
* 定义资源1和资源2 lock1和lock2
*/
private static Object lock1 = new Object();
private static Object lock2 = new Object();
private String name; // 线程名称
private boolean run; // 执行顺序标记
public DeadThread(String name,boolean run){
this.name = name;
this.run = run;
}
@Override
public void run() {
if(this.run){
// 线程1先占用资源1
synchronized(lock1){
try {
System.out.println("thread1 used lock1");
Thread.sleep(3000); // 暂时休眠3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 线程1再去申请资源2,此时资源2已经被线程2占用着不放了
synchronized(lock2){
System.out.println("hello dead-lock");
}
}
}else{
// 线程2先占用资源2
synchronized(lock2){
try {
System.out.println("thread2 used lock2");
Thread.sleep(3000); // 线程2暂时休眠3秒
} catch (InterruptedException e) {
e.printStackTrace();
}
// 线程2再去申请资源1,此时资源1已经被线程1占用着不放了
synchronized(lock1){
System.out.println("hello dead-lock");
}
}
}
}
}
}
class MyThread1 implements Runnable {
private boolean flag = true; // 定义标志位
public void run() {
int i = 0;
while (this.flag) {
System.out.println(Thread.currentThread().getName() + "运行, i = "
+ (i++));
}
}
public void stop() {
this.flag = false; // 修改标志位
}
}
/**
* @author Administrator
*
* @description 通过修改标记位停止线程
* @history
*/
public class ThreadStopDemo {
public static void main(String[] args) {
MyThread1 my = new MyThread1();
Thread t = new Thread(my, "线程"); // 建立线程对象
t.start(); // 启动线程
try {
Thread.sleep(50); // 适当地延迟下
} catch (Exception e) {
e.printStackTrace();
}

   my.stop();//修改标志位,停止运行

}

}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!