Thread
-
线程
java.lang.Thread
public class Thread extends Object implements Runnable
基本概念
- 程序
一段指令,完成一个特定的功能或一组特定的功能。
- 进程
一个程序被双击执行后,变成对应的进程
- 线程
是线程的一个执行单元,进程应用线程做事,一个进程有多个线程,如果一个进程只有一个线程称之为单进程/单线程程序。
- java采用的是多线程开发,python采用多进程开发。
- 调用
start
方法启动线程
创建线程
1:继承
1:继承Thread
2:重写run方法 run方法中编写要执行的逻辑代码
3:创建子类对象
4:调用start方法 启动该线程
public static void main(String[] args) {//main主线程
MyT m = new MyT();
m.start();
for(int i=0;i<5;i++) {
System.out.println("main"+i);
}
}
}
class MyT extends Thread{
@Override
public void run() {
for(int i=0;i<5;i++) {
System.out.println("myt"+i);
}
}
}
2:实现
1:实现Runnable
2:重写run方法 run方法中编写要执行的逻辑代码
3:创建Thread对象,将实现类对象作为参数传入Thread的构造器
4:调用start方法 启动该线程
public static void main(String[] args) {
MyRun m = new MyRun();
Thread t = new Thread(m);
t.start();
for(int i=0;i<5;i++) {
System.out.println("main"+i);
}
}
}
class MyRun implements Runnable{
@Override
public void run() {
for(int i=0;i<5;i++) {
System.out.println("MyT"+i);
}
}
}
3:实现
1:Callable接口
2:线程池
4:创建匿名内部类线程
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"下开始发车"+i+"号车出发");
}
}
},"紧急情况");
t1.start();
让线程变阻塞
-
方法
Thread.sleep();
t1.join
此时t1做完t2才执行;Thread.yield();
线程礼让public static void main(String[] args) { Thread t = new Thread(new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); for(int i=0;i<10000000;i++) { Thread.yield(); } long end = System.currentTimeMillis(); System.out.println(end-start); } }); t.start(); }
线程的同步
同步器,锁机制
synchronized
-
修饰非静态方法:
此时同一时间只有一个线程能访问该方法,该线程执行的方法完后才会将锁释放,其他的线程才能继续执行;
对于非静态方法synchronized锁的对象其实是this对象
-
修饰静态方法时
synchronized锁的其实是当前类的class对象
-
同步代码块
synchronized(锁芯){
}
锁芯:对象
非静态方法使用 首选this对象
静态方法使用 首选类的class对象
public class Test02 {
public static void main(String[] args) {
TicketRun tr = new TicketRun();
for(int i=65;i<70;i++) {
new Thread(tr,"窗口"+(char)i).start();
}
}
}
class TicketRun implements Runnable{
int count = 10;
@Override
public void run() {
for(int i=0;i<10;i++) {
checkTicket();
}
}
public synchronized void checkTicket() {
if(count>0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在售票:"+(count--));
}
}
}
标准的单例模式
public static void main(String[] args) {
for(int i=0;i<5;i++) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Lazy.getInstance());
}
}).start();
}
}
}
class Lazy{
private volatile static Lazy lazy = null;
private Lazy() {
if(lazy!=null) {
throw new RuntimeException("别反射");
}
}
public static Lazy getInstance() {
if(lazy==null) {
synchronized (Lazy.class) {
if(lazy==null) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
lazy = new Lazy();
}
}
}
return lazy;
}
public Object readResolve(){
return lazy;
}