Java学习笔记<8>线程

时间:2022-06-12 19:42:48

线程

 

 

 

线程的基本概念

 

<1>java线程是java.lang.Thread类来实现的

<2>可以通过创建Thead类的实例来创建新线程

<3>每个线程通过某个特定Thread类的实例所对应的run方法来完成其操作。

<4>通过调用Thread类的start方法来启动一个线程

 

 

 

 

线程的创建和启动

 

<1>定义线程类实现Runnable接口

<2>继承Thread类并重写run方法

 

Java学习笔记<8>线程

 

 

 

 

举例:

 

 

<1>继承方式实现

 

public class TestThread1 {
public static void main(String args[]) {
Runner1 r = new Runner1();
r.start();
//r.run();
//Thread t = new Thread(r);
//t.start();

for(int i=0; i<100; i++) {
System.out.println("Main Thread:------" + i);
}
}
}

//class Runner1 implements Runnable {
class Runner1 extends Thread {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("Runner1 :" + i);
}
}
}

 

 


<2>实现接口方式实现

 

public class TestThread2 {
public static void main(String args[]) {
Runner2 r = new Runner2();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
}
}

class Runner2 implements Runnable {
public void run() {
for(int i=0; i<30; i++) {
System.out.println("No. " + i);
}
}
}


 

 

 

线程的状态转换

 

Java学习笔记<8>线程

 

 

 

 

线程的基本控制方法

 

Java学习笔记<8>线程

 

 

 

sleep方法

 

<1>静态方法 public static void sleep(long millis)  throws InterruptedException

<2>可由Thread.sleep(...)直接调用,使当前线程休眠

 

举例:

 

import java.util.*;
public class TestInterrupt {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
try {Thread.sleep(10000);}
catch (InterruptedException e) {}
thread.interrupt();
}
}

class MyThread extends Thread {
boolean flag = true;
public void run(){
while(flag){
System.out.println("==="+new Date()+"===");
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}


 

结果:

 

 

Java学习笔记<8>线程

 

 

 

 

join方法

 

合并某个线程

 

举例:

 

public class TestJoin {
public static void main(String[] args) {
MyThread2 t1 = new MyThread2("abcde");
t1.start();
try {
t1.join();
} catch (InterruptedException e) {}

for(int i=1;i<=10;i++){
System.out.println("i am main thread");
}
}
}
class MyThread2 extends Thread {
MyThread2(String s){
super(s);
}

public void run(){
for(int i =1;i<=10;i++){
System.out.println("i am "+getName());
try {
sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}


结果:

 

 

Java学习笔记<8>线程

 

 

yeild方法

 

让出CPU,给其他线程执行的机会

 

举例:

 

public class TestYield {
public static void main(String[] args) {
MyThread3 t1 = new MyThread3("t1");
MyThread3 t2 = new MyThread3("t2");
t1.start(); t2.start();
}
}
class MyThread3 extends Thread {
MyThread3(String s){super(s);}
public void run(){
for(int i =1;i<=100;i++){
System.out.println(getName()+": "+i);
if(i%10==0){
yield();
}
}
}
}


 

 

 

 

线程优先级

 

<1>java线程优先级用数字表示,范围从1-10,默认为5。

<2>通过下面方法得到或设置线程的优先级

 

Java学习笔记<8>线程

 

 

 

举例:

 

public class TestPriority {
public static void main(String[] args) {
Thread t1 = new Thread(new T1());
Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3);//NORM_PRIORITY 为默认优先级
t1.start();
t2.start();
}
}

class T1 implements Runnable {
public void run() {
for(int i=0; i<1000; i++) {
System.out.println("T1: " + i);
}
}
}

class T2 implements Runnable {
public void run() {
for(int i=0; i<1000; i++) {
System.out.println("------T2: " + i);
}
}
}


 

 

 

 

 

线程同步

 

<1>通过对象互斥锁,保证共享数据的完整性。

<2>每个对象都对应一个成为互斥锁的标记,这个标记在任一时刻,只允许一个线程访问。

<3>关键字synchronized来与对象互斥锁联系,当某个对象被该关键字修饰时,表明该对象在任一时刻只能被一个

线程访问。

<4>synchronized的使用

(1)synchronized(this) { ...  }

(2)还可以放在方法声明中 synchronized public void .....

 

 

 

举例:生产者消费者模型

public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
}
}

class WoTou {
int id;
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}

class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];

public synchronized void push(WoTou wt) {
while(index == arrWT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}

public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}

class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}

public void run() {
for(int i=0; i<60; i++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}