JAVA多线程

多线程

前言:学完多线程,感觉有点难??????????

java.lang.Thread

创建线程的方式:

public class ThreadTest {
	//1.通过继承Thread创建线程对象
	public static void main(String[] args) {
//		MyThr m = new MyThr();
//		m.start();
//		for(int i=0;i<20;i++)
//			System.out.println("我是main方法");
		//2.通过实现Runnalbe接口创建线程对象
		MyThr1 m1 = new MyThr1();
		Thread t = new Thread(m1);
		t.start();
		for(int i=0;i<20;i++)
			System.out.println("我也是main方法");
	}
}
class MyThr extends Thread{
	public void run() {
		for(int i=1;i<=5;i++) {
			System.out.println("我是线程对象");
		}
		
	}
}
class MyThr1 implements Runnable{
	@Override
	public void run() {
		for(int i=1;i<=5;i++) {
			System.out.println("我也是线程对象");
		}
	}
	
}

构造器

ConstructorDescription
Thread()Allocates a new Thread object.
Thread(Runnable target)Allocates a new Thread object.
Thread(Runnable target, String name)Allocates a new Thread object.
Thread(String name)Allocates a new Thread object.

Thread中的三个静态属性

MAX_PRIORITY:10

MIN_PRIORITY:1

NORM_PRIORITY:5

常见方法

返回值方法名作用
static ThreadcurrentThread()返回当前正在执行的线程对象的引用
StringgetName()返回此线程的名字
intgetPriority()返回此线程的优先级
voidinterrupt()中断此线程
booleanisAlive()测试此线程是否存活
booleandaemon()测试是否为守护线程
返回值方法名作用
voidjoin()某个线程加入,等待加入的线程死亡
voidsetName(String name)修改线程名
voidsetPriority(int p)修改优先级
static voidsleep(long millis)使线程休眠的毫秒数
static voidyield()将线程占用的资源让出去

生命周期

在这里插入图片描述

线程的同步策略

synchronized

加锁机制:(有待学习)

public class ThreadTest2 {
	public static void main(String[] args) {
		for (int i = 65; i < 70; i++) {
			new TicketThread("窗口" + (char) i).start();
		}
	}
}

class TicketThread extends Thread {
	static int count = 10;

	public TicketThread(String name) {
		super(name);
	}

	public void run() {
		for (int i = 0; i < 10; i++) {
			synchronized ("1") {
				if (count > 0) {
					try {
						Thread.sleep(2);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					System.out.println(this.getName() + "正在售票" + (count--));
				}
			}
		}
	}
}

volatile

多线程的内存模型:main memory内存,working memory线程栈。在处理数据时,线程会把内存中值从内存load到本地,完成操作后再save回去

volatile关键字的作用:每次针对该变量的操作都激发一次load and save

针对多线程使用的变量如果不是volatile修饰的,很有可能会出错(另一个线程修改了这个值,但是之后在某线程看到的值是修改之前的值)

public class ThreadTest3 {
	public static void main(String[] args) {
		T t = new T();
		t.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t.flag = false;
	}
}
class T extends Thread {
	volatile boolean flag = true;
	@Override
	public void run() {
		System.out.println("while循环开始了。。。。。。");
		while (flag) {
		}
		System.out.println("while循环结束了了。。。。。。");

	}
}
@Override
public void run() {
	System.out.println("while循环开始了。。。。。。");
	while (flag) {
	}
	System.out.println("while循环结束了了。。。。。。");

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值