线程:
线程就是程序执行的一条路径,一个进程中可以包含多条线程
多条线程并发执行可以提高程序的效率,可以同时完成多项工作
实现方法:
第一种:继承Thread
public class Demo1_Thread {
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.start();
for(int i=0;i<1000;i++){
System.out.println("bbbbbb");
}
}
}
class MyThread extends Thread{
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("aaaaaaaaaaaaaa");
}
}
}
第二种:实现Runnable接口
public class Demo2_Thread {
public static void main(String[] args) {
MThread mt=new MThread();
new Thread(mt).start();
for(int i=0;i<1000;i++){
System.out.println("bbb");
}
}
}
class MThread implements Runnable{
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("aaaaaaaaaaaaaa");
}
}
}
线程比较常用的方法:
getName():得到当前线程的名字
sleep():等待传入的毫秒的时间之后继续执行
join():当前线程暂停,等待指定的线程执行后,当前线程再执行
currentThread():获取当前线程的对象
public class Demo3 {
public static void main(String[] args) {
Thread t1=new Thread(){
public void run(){
for(int i=0;i<10;i++){
System.out.println(this.getName()+":aaaaaaaaa");
}
}
};
Thread t2=new Thread(){
public void run(){
for(int i=0;i<10;i++){
if(i==2){
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.getName()+":bb");
}
}
};
t1.start();
t2.start();
}
}
线程—火车站购票
public class Test1 {
/**
* 模拟火车站购票
* @param args
*/
public static void main(String[] args) {
Ticket t=new Ticket();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}
class Ticket implements Runnable{
private int tickets=100;
@Override
public void run() {
while(true){
synchronized (Ticket.this) {
if(tickets<=0){
break;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"这是第"+tickets--+"号票");
}
}
}
}
互斥锁:防止线程并发
ReentrantLock:
lock():获取锁
unlock():释放锁
Condition:
await():处于等待状态。
signal():唤醒一个等待线程。
public class Demo4_lock {
public static void main(String[] args) {
Mylock m = new Mylock();
new Thread() {
public void run() {
while (true) {
try {
m.print1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
m.print2();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while (true) {
try {
m.print3();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
class Mylock {
private int i = 1;
private ReentrantLock rtl = new ReentrantLock();
private Condition cd1 = rtl.newCondition();
private Condition cd2 = rtl.newCondition();
private Condition cd3 = rtl.newCondition();
public void print1() throws InterruptedException {
rtl.lock();
if (i != 1) {
cd1.await();
}
System.out.print("你");
System.out.print("好");
System.out.print("呀");
System.out.println();
i = 2;
cd2.signal();
rtl.unlock();
}
public void print2() throws InterruptedException {
rtl.lock();
if (i != 2) {
cd2.await();
}
System.out.print("臭");
System.out.print("宁");
System.out.print("宁");
System.out.println();
i = 3;
cd3.signal();
rtl.unlock();
}
public void print3() throws InterruptedException {
rtl.lock();
if (i != 3) {
cd3.await();
}
System.out.print("不");
System.out.print("知");
System.out.print("道");
System.out.println();
i = 1;
cd1.signal();
rtl.unlock();
}
}