1. 类继承Thread类,[重写run()方法,]调用start(),不适合资源共享。
public class ThreadDemo1 extends Thread{
//添加构造方法,方便区分新的线程
public ThreadDemo1(String name) {
super(name);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+i);
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo1 demo1 = new ThreadDemo1("小强");
demo1.start();
System.out.println(Thread.currentThread().getName()+i);
}
}
2. 类实现Runable接口,[重写run()方法,]调用start()
public class Runable1 implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "***" + i);
}
public static void main(String[] args) {
Runable1 runable1 = new Runable1();
Thread thread = new Thread(runable1);
thread.start();
}
**3.使用匿名内部类(与方法2实现相同,只是重写的地方不一样)**创建Thread对象传入参数(Runable接口)Runable为匿名内部类,同时重写Runable中的Run()方法。最后调用Thread对象的start()方法
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
});
thread.start();
线程体现的是同一时间多个方法栈同时工作。为体现这一效果添加了for (int i = 0; i < 10; i++)多输出几次,也可以再配合thread.sleep(毫秒数)。
同步锁
- 同步代码块(推荐)
//锁对象Object,该类中有wait(释放锁对象,程序停止)和notify(通知当前在此锁对象上的线程 唤醒运行)
//使用wait和notify时候为保证锁对象一样,还是要使用同步代码快的方式,而不能使用同步方法
//ReentrantLock 类中没有wait、notify方法
private final Object obj=new Object();
//只要是同一个Monitor就行,不用管 Monitor的属性是否变化,那就不用final修饰了
@Override
public void run() {
//同步代码块
synchronized (obj){
}
- 同步方法
//同步方法 锁对象this
public synchronized void sellTicket() {
}
- ReentrantLock
private ReentrantLock lock=new ReentrantLock();
//在需要同步锁的开始位置添加
lock.lock();
//结束位置
lock.unlock();
线程的状态
可参阅java.lang.Thread 的public enum State
A thread can be in one of the following states:
线程可以处于以下状态之一:
NEW
A thread that has not yet started is in this state.
NEW尚未启动的线程处于此状态。
RUNNABLE
A thread executing in the Java virtual machine is in this state.
RUNNABLE在Java虚拟机上执行的线程处于这种状态。
BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
正在等待监视器锁的阻塞线程处于这种状态。
WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
正在无限期等待另一个线程执行某个特定操作的线程处于这种状态。
TIMED_WAITING
A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
在指定的等待时间内等待另一个线程执行某个操作的线程处于这种状态。
TERMINATED
A thread that has exited is in this state.
已退出的线程处于此状态。
A thread can be in only one state at a given point in time.
在给定的时间点,线程只能处于一种状态。
These states are virtual machine states which do not reflect any operating system thread states.
这些状态是不反映任何操作系统线程状态的虚拟机状态。
Since:1.5
See Also:
getState
使用CountDownLatch实现线程同步
import java.util.concurrent.CountDownLatch;
public class CountDownLatchDemo {
static class TaskThread extends Thread {
CountDownLatch latch;
public TaskThread(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void run() {
//处理业务
System.out.println(Thread.currentThread().getName() + " Task is Done");
// 计数减一
latch.countDown();
}
}
public static void main(String[] args) throws InterruptedException {
int threadNum = 10;
CountDownLatch latch = new CountDownLatch(threadNum);
System.out.println("Task Start!");
for (int i = 0; i < threadNum; i++) {
TaskThread task = new TaskThread(latch);
task.start();
}
// 如果 new CountDownLatch(threadNum)的当前计数大于零,则当前线程出于线程调度目的而被禁用并处于休眠状
// 解除休眠状态的 条件一:调用countDown方法,计数达到零 条件二:其他线程中断当前线程
latch.await();
System.out.println("All Task is Done!");
}
}
关键代码
在创建CountDownLatch时候保证创建的线程数与 构造方法的 count一致。在创建线程实现run()方法的最后一步 使用
latch.countDown(); 使计数器减一
。
为保证创建线程都执行过了
在最外层使用latch.await();
版权声明:本文为qq_34922830原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。