线程
线程实现方法:Thread类,继承Runnable接口。但是Runnable需要用Thread实例。
优先级
<span style="font-size:14px;">class RunnableTest implements Runnable{ public void run() { // TODO Auto-generated method stub for(int j=0;j<5;j++){ try { Thread.sleep(1000); System.out.print(Thread.currentThread().getName()+":"+j+" "); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class RunnableDome { public static void main(String[] args) { // TODO Auto-generated method stub Thread t1=new Thread(new RunnableTest(),"A"); Thread t2=new Thread(new RunnableTest(),"B"); Thread t3=new Thread(new RunnableTest(),"c"); //设置优先级 t1.setPriority(Thread.MIN_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t3.setPriority(Thread.NORM_PRIORITY); t1.start();t2.start();t3.start(); } }</span>
![]()
线程常用方法
<span style="font-size:14px;">public class ThreadDemo extends Thread{ String name; public ThreadDemo(String name){ this.name=name; } //必须重写run()方法,把功能代码放入 public void run(){ System.out.println("返回当前线程的线程组中活动线程的数目:"+Thread.activeCount()); System.out.println("返回对当前正在执行的线程对象的引用:"+Thread.currentThread()); System.out.println("返回该线程的名称:"+Thread.currentThread().getName()); for(int j=0;j<10;j++){ if(j>5&&j<8){ //暂停当前正在执行的线程对象,并执行其他线程 yield(); System.out.println(name+":"+j); } else if(j>8){ try { //join()等待该线程终止 join(); System.out.println("name:"+j); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ try { //sleep()方法是指定的毫秒数内让当前正在执行的线程休眠。 sleep(1000); System.out.println(name+":"+j); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) { // TODO Auto-generated method stub //创建线程 ThreadDemo t1=new ThreadDemo("A"); ThreadDemo t2=new ThreadDemo("B"); //启动线程方法start()方法。几个线程时,谁抢到资源谁先执行 t1.start(); t2.start(); System.out.println("测试线程是否处于活动状态:"+Thread.currentThread().isAlive()); } }</span>
![]()
线程的同步机制synchronized
1.同步代码块
synchronized(同步对象){
需要同步的代码
}
<span style="font-size:14px;">class Test implements Runnable{ //定义车票数量,好为下面进行判断 private int ticket=10; public void run() { // TODO Auto-generated method stub for(int j=1;j<11;j++){ synchronized(this){ if(ticket>0){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("车票:"+ticket--); } } } } } public class Synchronized01 { public static void main(String[] args) { // TODO Auto-generated method stub <span style="color:#FF0000;">//三个窗口同时卖10张票,这时候,实例对象,Thread线程的对象必须是同一个</span> Test t=new Test(); Thread t1=new Thread(t); Thread t2=new Thread(t); Thread t3=new Thread(t); t1.start();t2.start();t3.start(); } }</span>
![]()
2.同步方法
synchronized void 方法名(){
}
class Test implements Runnable{ private int ticket=10; public void run() { // TODO Auto-generated method stub for(int j=1;j<11;j++){ tell(); } } synchronized void tell(){ if(ticket>0){ try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("车票:"+ticket--); } } } public class Synchronized01 { public static void main(String[] args) { // TODO Auto-generated method stub //三个窗口同时卖10张票 Test t=new Test(); Thread t1=new Thread(t); Thread t2=new Thread(t); Thread t3=new Thread(t); t1.start();t2.start();t3.start(); } }
效果和同步代码块一样