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()方法 }};
第二种:继承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() ; // 启动线程 }};
2、多线程的使用第一种:实现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() ; // 启动多线程 }};
第二种: 继承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() ; // 调用线程主体 }};
3、线程同步如何使用: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() ; }};
4、线程锁的使用: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() ; }};