1、继承Thread创建线程
public class ThreadDemo { public static void main(String[] args) { ThreadTest1 st1=new ThreadTest1("张三"); ThreadTest1 st2=new ThreadTest1("李四"); //调用start方法开启线程 st1.start(); st2.start(); System.out.PRintln(" main over...."+Thread.currentThread().getName()); }}class ThreadTest1 extends Thread{ String name; public ThreadTest1(String name) { this.name=name; } //run方法定义线程要运行的任务代码 public void run(){ for (int i = 0; i < 10; i++) { System.out.println(i+"......"+name+"....."+Thread.currentThread().getName()); } }}线程的状态:
sleep:到时间自动回到运行状态
wait:不调用线程的notify方法则一直处于冻结状态,冻结状态是指任务被移出CPU任务处理队列
2、继承Runnable接口创建线程
//继承Runnable接口创建线程public class RunnableDemo {public static void main(String[] args) { RunnableTest test=new RunnableTest(); //将Runnable接口的实现类作为参数传递给Thread的构造方法,用以明确线程的任务 Thread t1=new Thread(test); Thread t2=new Thread(test); t1.start(); t2.start();}}class RunnableTest implements Runnable{ @Override public void run() { show(); } public void show(){ for (int i = 0; i < 10; i++) { System.out.println(i+"...."+Thread.currentThread().getName()); } }}卖票问题:同步代码块:
//卖票问题//线程安全问题产生的原因:多个线程在操作共享数据,且操作共享数据的线程代码有多条,当一个线程操作多条共享代码的过程中,其他进程参与了运算,//就产生了线程安全问题//利用同步代码块可以解决线程安全问题synchronized//同步的前提是多线程必须持有同一个锁public class SaleTicket { public static void main(String[] args) { SaleTicketDemo saleTicketDemo=new SaleTicketDemo(); //四个线程执行同一个任务 Thread t1=new Thread(saleTicketDemo); Thread t2=new Thread(saleTicketDemo); Thread t3=new Thread(saleTicketDemo); Thread t4=new Thread(saleTicketDemo); t1.start(); t2.start(); t3.start(); t4.start(); }}class SaleTicketDemo implements Runnable{ private int num=100; Object obj=new Object();//同步锁对象必须放在run方法以外 @Override public void run() { while(true){ synchronized (obj) { if(num>0){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"....."+num--); } } } } }}同步函数:
public class SaleTicket { public static void main(String[] args) { SaleTicketDemo saleTicketDemo=new SaleTicketDemo(); //四个线程执行同一个任务 Thread t1=new Thread(saleTicketDemo); Thread t2=new Thread(saleTicketDemo); Thread t3=new Thread(saleTicketDemo); Thread t4=new Thread(saleTicketDemo); t1.start(); t2.start(); t3.start(); t4.start(); }}class SaleTicketDemo implements Runnable{ private int num=100; Object obj=new Object();//同步锁对象必须放在run方法以外 @Override public void run() { while(true){ show(); } } //同步函数,同步函数的锁是this public synchronized void show(){ if(num>0){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"....."+num--); } } }线程之间的通信:
package Thread;/* * 当线程执行wait()时,会把当前的锁释放,然后让出CPU,进入等待状态。 * 当执行notify/notifyAll方法时,会唤醒一个处于等待该 对象锁 的线程,然后继续往下执行,直到执行完退出对象锁锁住的区域(synchronized修饰的代码块)后再释放锁。 * * */class Resource{ String name; String sex; //资源中是否含有数据,false为不含数据 boolean flag=false;}class Input implements Runnable{ Resource r; public Input(Resource r) { this.r = r; } @Override public void run() { int x=0; while(true){ synchronized (r) { if(r.flag){ try { r.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else{ if(x==0){ r.name="张三"; r.sex="男"; }else{ r.name="李四"; r.sex="女"; } r.flag=true; r.notify(); x=(x+1)%2; } } } } }class Output implements Runnable{ Resource r; public Output(Resource r) { this.r = r; } @Override public void run() { while(true){ synchronized (r) { if(!r.flag){ try { r.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else{ System.out.println(r.name+"....."+r.sex); r.flag=false; r.notify(); } } } } }public class ThreadTalk { public static void main(String[] args) { Resource resource=new Resource(); Input input=new Input(resource); Output output=new Output(resource); Thread t1=new Thread(input); Thread t2=new Thread(output); t1.start(); t2.start(); }}
新闻热点
疑难解答