Javad 的线程是通过java.lang.Thread类来实现的
VM启动时会有一个由主方法(public static void main(){})suo dingyi d xiancheng所定义的线程 .
可以通过创建Thread的实例来创建新的线程
每个线程都是通过某个特定Thread对象所对应的方法run()来完成其操作的,方法run()成为线程体。
通过调用Thread类的start()方法来启动一个线程。
创建线程的两种方法:
方法一:(good)
public class TestThread1{
public static void main(String []args){
Runner1 r=new Runner1();
Thread t=new Thread(r);
t.start();
for(int i=0;i<100;i++){
System.out.println("main"+i);
}
}
}
class Runner1 implements Runnable{
public void run(){
for(int i=0;i<100;i++){
System.out.println("Runner1"+i);
}
}
}
方法二:
public class TestThread2{
public static void main(String []args){
Runner1 r=new Runner1();
r.start();
for(int i=0;i<100;i++){
System.out.println("main-------"+i);
}
}
}
class Runner1 extends Thread{
public void run(){
for(int i=0;i<100;i++){
System.out.println("Runner1-------"+i);
}
}
}
线程控制基本方法:
方法 功能
isAlive() 判断线程是否还活着,即线程是否还未终止
getPriority() 获得线程的优先级数值
setPriority() 设置线程的优先级数值
Thread.sleep() 将当前线程睡眠指定毫秒数
join() 调用某线程的该方法,将当前线程与该线程“合并”,即等待该线程结束,再回复当前线程的运行。
yield() 让出CPU,当前线程进入就绪队列等待调度
wait() 当前线程进入对象的wait pool
notify()/notifyAll() 唤醒对象的wait pool中的一个/所有等待线程。
sleep方法
sleep()由于是静态方法,可以由类名直接调用:Thread.sleep()
sleep() Example
import java.util.*;
public class TestSleep{
public static void main(String []args){
Runner r=new Runner();
r.start();
try{
Thread.sleep(10000); //这里是让主线程睡眠10秒
//sleep是静态方法,注意其使用!
}catch(InterruptedException e){}
r.interrupt();
}
}
class Runner extends Thread{
public void run(){
while(true){
System.out.println("---"+new Date()+"---");
try{sleep(1000);}
catch(InterruptedException e){
return ;
}
}
}
}
join方法
合并某个线程
yield方法
让出CPU,给其他线程执行的机会
join example
public class TestJoin{
public static void main(String []args){
Runner r=new Runner("mythread");
r.start();
try{r.join();
}catch(InterruptedException e){}
for(int i=0;i<10;i++){
System.out.println("I am main thread");
}
}
}
class Runner extends Thread{
Runner(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;}
}
}
}
yield example
public class TestYield{
public static void main(String []args){
Mythread t1=new Mythread("abc-----");
Mythread t2=new Mythread("def");
t1.start();
t2.start();
}
}
class Mythread extends Thread{
Mythread(String s){super(s);}
public void run(){
for(int i=1;i<=100;i++){
System.out.println(getName()+": "+i);
if(i%10==0)yield();
}
}
}
线程的优先级别
java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程。线程调度器按照线程的优先级别决定应调度哪个线程来执行。
线程的有限级别用数字表示,范围从1到10,一个线程的缺省优先级是5.
Thread.MIN_PRIORITY=1;
Thread.MAX_PRIORITY=10; (静态变量)
Thread.NORM_PRIORITY=5;
example [为什么注释起来的写法不可以呢???]
public class TestPriority{
public static void main(String []args){
/*
Mythread t1=new Mythread("abc-----");
Mythread t2=new Mythread("def");
t1.setPriority(Thread.NORM_PRIORITY+3);
t1.start();
t2.start();
}
*/
Thread t1=new Thread(new T1());
Thread t2=new Thread(new T2());
t1.setPriority(9);
//setPriority()放在start()之前!!!
t1.start();
t2.start();
}
}
/*
class Mythread extends Thread{
Mythread(String s){super(s);}
public void run(){
for(int i=1;i<=10;i++){
System.out.println(getName()+": "+i);
}
}
}
*/
class T1 implements Runnable{
public void run(){
for(int i=0;i<100;i++)
System.out.println("T1: "+i);
}
}
class T2 implements Runnable{
public void run(){
for(int i=0;i<100;i++)
System.out.println("T2: "+i);
}
}
一些小例子:
public class TestThread11{
public static void main(String []args){
runner r=new runner();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start();
}
}
class runner implements Runnable{
public void run(){
for(int i =0;i<30;i++){
System.out.println("No."+i);
}
}
}
public class TestThread22{
public static void main(String args[]){
Runner r=new Runner();
Thread t=new Thread(r);
t.start();
}
}
class Runner implements Runnable{
public void run(){
for(int i=0;i<30;i++){
if(i%10==0&&i!=0){
try{
Thread.sleep(2000);
}catch(InterruptedException e){}
}
System.out.println("No."+i);
}
}
}
public class TestThread3{
public static void main(String []args){
mythread r=new mythread();
Thread t=new Thread(r);
t.start();
for(int i=0;i<100;i++){
System.out.println("--------------main: "+i);
}
System.out.println("thread main is over! ");
r.shutdown(); //此处不是t.shutdown() !!!!
}
}
class mythread implements Runnable{
private boolean flag=true;
public void run(){
int i=0;
while(flag){
System.out.println("mythread: "+i++);
}
}
public void shutdown(){
flag=false;
}
}
public class TestThread4{
public static void main(String []args){
Thread t=new Thread(new mythread());
t.start();
for(int i=0;i<30;i++){
System.out.println("----------MainThread:"+i);
}
}
}
class mythread implements Runnable{
public void run(){
System.out.println(Thread.currentThread().isAlive());
for(int i=0;i<30;i++){
System.out.println("SubThread:"+i);
}
}
}
线程同步
线程同步
public class TestSync implements Runnable{
Timer timer=new Timer();
public static void main(String []args){
TestSync test=new TestSync();
Thread t1=new Thread(test);
Thread t2=new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num;
public synchronized void add(String name){
// synchronized(this){
num++;
try{
Thread.sleep(1);
}catch(InterruptedException e){}
System.out.println(name+",你是第"+num+"个使用timer的线程");
// }
}
}
死锁
public class TestDeadLock implements Runnable{
private int flag=1;
static Object o1=new Object();
static Object o2=new Object();
public void run(){
System.out.println("flag="+flag);
if(flag==1){
synchronized(o1){
try{
Thread.sleep(500);
}catch(Exception e){}
synchronized(o2){
System.out.println(1);
}
}
}
if(flag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(Exception e){}
synchronized(o1){
System.out.println(0);
}
}
}
}
public static void main(String []args){
TestDeadLock td1=new TestDeadLock();
TestDeadLock td2=new TestDeadLock();
td1.flag=1;
td2.flag=0;
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t2.start();
}
}
注意:在下面的程序中,m1方法加了锁,在m1方法执行期间,由于m2方法没有加锁,所以另外的线程可以去执行m2方法!只有当m2方法也加了锁后,另外的线程必须等原先线程执行完m1后才能执行m2!
public class TT implements Runnable{
int b=100;
public synchronized void m1() throws Exception{
b=1000;
Thread.sleep(5000);
System.out.println("b="+b);
}
public (synchronized) void m2(){
System.out.println(b);
}
public void run(){
try{
m1();
}catch(Exception e){}
}
public static void main(String []args) throws Exception{
TT tt=new TT();
Thread t=new Thread(tt);
t.start();
Thread.sleep(1000);
tt.m2();
}
}
程序执行结果: 当public void m2(){。。。}时:
1000
b=1000
当public synchronized void m2(){。。。}时:
b=1000
1000
example, 针对下面两张情况的结果
public class TT implements Runnable{
int b=100;
public synchronized void m1() throws Exception{
b=1000;
Thread.sleep(5000);
System.out.println("b="+b);
}
public (synchronized) void m2()throws Exception{
Thread.sleep(2500);
b=2000;
}
public void run(){
try{
m1();
}catch(Exception e){}
}
public static void main(String []args) throws Exception{
TT tt=new TT();
Thread t=new Thread(tt);
t.start();
Thread.sleep(1000);
tt.m2();
System.out.println(tt.b);
}
}
生产者和消费者问题:
wait 和sleep区别:
wait时别的线程可以访问锁定对象,调用wait方法时必须锁定该对象
sleep时别的线程也不可以访问锁定对象