1、JAVA线程创建有两种方法:第一种:实现Runnable接口:例如:class MyThread implements Runnable{// 实现Runnable接口public void run(){// 覆写run()方法for(int i=0;i<3;i++){System.out.println(Thread.currentThread().getName()+"运行,i = " +i) ;// 取得当前线程的名字}}};public class CurrentThreadDemo{public static void main(String args[]){MyThread mt = new MyThread() ;// 实例化Runnable子类对象new Thread(mt,"线程").start() ;// 启动线程mt.run() ;// 直接调用run()方法}};
2、第二种:继承Thread 类class MyThread extends Thread{private int time ;public MyThread(String name,int time){super(name) ;// 设置线程名称this.time = time ;// 设置休眠时间}public void run(){try{Thread.sleep(this.time) ;// 休眠指定的时间}catch(InterruptedException e){e.printStackTrace() ;}System.out.println(Thread.currentThread().getName() + "线程,休眠"+ this.time + "毫秒。") ;}};public class ExecDemo01{public static void main(String args[]){MyThread mt1 = new MyThread("线程A",10000) ;// 定义线程对象,指定休眠时间MyThread mt2 = new MyThread("线程B",20000) ;// 定义线程对象,指定休眠时间MyThread mt3 = new MyThread("线程C",30000) ;// 定义线程对象,指定休眠时间mt1.start() ;// 启动线程mt2.start() ;// 启动线程mt3.start() ;// 启动线程}};
3、多线程的使用第一种:实现Runnable接口class MyThread implements Runnable{// 实现Runnable接口,作为线程的实现类private String name ;// 表示线程的名称public MyThread(String name){this.name = name ;// 通过构造方法配置name属性}public void run(){// 覆写run()方法,作为线程 的操作主体for(int i=0;i<10;i++){System.out.println(name + "运行,i = " + i) ;}}};public class RunnableDemo01{public static void main(String args[]){MyThread mt1 = new MyThread("线程A ") ; // 实例化对象MyThread mt2 = new MyThread("线程B ") ; // 实例化对象Thread t1 = new Thread(mt1) ;// 实例化Thread类对象Thread t2 = new Thread(mt2) ;// 实例化Thread类对象t1.start() ;// 启动多线程t2.start() ;// 启动多线程}};
4、第二种:继承Thread类class MyThread extends Thread{// 继承Thread类,作为线程的实现类private String name ;// 表示线程的名称public MyThread(String name){this.name = name ;// 通过构造方法配置name属性}public void run(){// 覆写run()方法,作为线程 的操作主体for(int i=0;i<10;i++){System.out.println(name + "运行,i = " + i) ;}}};public class ThreadDemo01{public static void main(String args[]){MyThread mt1 = new MyThread("线程A ") ; // 实例化对象MyThread mt2 = new MyThread("线程B ") ; // 实例化对象mt1.run() ;// 调用线程主体mt2.run() ;// 调用线程主体}};
5、线程同步如何使用:class MyThread implements Runnable{private int ticket = 5 ;// 假设一共有5张票public void run(){for(int i=0;i<100;i++){if(ticket>0){// 还有票try{Thread.sleep(300) ;// 加入延迟}catch(InterruptedException e){e.printStackTrace() ;}System.out.println("卖票:ticket = " + ticket-- );}}}};public class SyncDemo01{public static void main(String args[]){MyThread mt = new MyThread() ;// 定义线程对象Thread t1 = new Thread(mt) ;// 定义Thread对象Thread t2 = new Thread(mt) ;// 定义Thread对象Thread t3 = new Thread(mt) ;// 定义Thread对象t1.start() ;t2.start() ;t3.start() ;}};
6、线程锁的使用:class Zhangsan{// 定义张三类public void say(){System.out.println("张三对李四说:“你给我画,我就把书给你。”") ;}public void get(){System.out.println("张三得到画了。") ;}};class Lisi{// 定义李四类public void say(){System.out.println("李四对张三说:“你给我书,我就把画给你”") ;}public void get(){System.out.println("李四得到书了。") ;}};public class ThreadDeadLock implements Runnable{private static Zhangsan zs = new Zhangsan() ;// 实例化static型对象private static Lisi ls = new Lisi() ;// 实例化static型对象private boolean flag = false ;// 声明标志位,判断那个先说话public void run(){// 覆写run()方法if(flag){synchronized(zs){// 同步张三zs.say() ;try{Thread.sleep(500) ;}catch(InterruptedException e){e.printStackTrace() ;}synchronized(ls){zs.get() ;}}}else{synchronized(ls){ls.say() ;try{Thread.sleep(500) ;}catch(InterruptedException e){e.printStackTrace() ;}synchronized(zs){ls.get() ;}}}}public static void main(String args[]){ThreadDeadLock t1 = new ThreadDeadLock() ;// 控制张三ThreadDeadLock t2 = new ThreadDeadLock() ;// 控制李四t1.flag = true ;t2.flag = false ;Thread thA = new Thread(t1) ;Thread thB = new Thread(t2) ;thA.start() ;thB.start() ;}};
7、但是我们在开发中一般会使用多线程或线程池,多线程上面已经介绍了,线程池我们一般会用JAVA自带的线程池//创建一个可重用固定线程数的线程池ExecutorService pool = Executors. newSingleThreadExecutor();Thread t1 = new MyThread();Thread t2 = new MyThread();Thread t3 = new MyThread();Thread t4 = new MyThread();Thread t5 = new MyThread();pool.execute(t1);pool.execute(t2);pool.execute(t3);pool.execute(t4);pool.execute(t5);//关闭线程池pool.shutdown();