线程的实现:
1.继承Thread 类,重写run函数
package demo2; public class Demo1 { public static void main(String[] args) { Cat cat = new Cat(); cat.start(); } } class Cat extends Thread { int times = 0; // 重写run函数 public void run() { while (true) { // 每一秒打印一次 try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } System.out.println("hello,world"); times++; if (times == 10) break; } } }
2.实现Runable接口,重写run函数
package demo2; public class Demo1 { public static void main(String[] args) { Cat cat = new Cat(); Thread t = new Thread(cat); t.start(); } } class Cat implements Runnable { int times = 0; // 重写run函数 public void run() { while (true) { // 每一秒打印一次 try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } System.out.println("hello,world"); times++; if (times == 10) break; } } }
3.多线程运作
package demo2; public class Demo2 { public static void main(String[] args) { Pig pig = new Pig(10); Bird bird = new Bird(10); Thread t1 = new Thread(pig); Thread t2 = new Thread(bird); t1.start(); t2.start(); } } class Bird implements Runnable { int n, ret = 0, times = 0; public Bird(int times) { this.times = times; } @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } System.out.println("飞出来一只鸟"); ret++; if (times == ret) break; } } } class Pig implements Runnable { int n, ret = 0, times = 0; public Pig(int times) { this.times = times; } @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } System.out.println("飞出来一只猫"); ret++; if (times == ret) break; } } }
深入理解:
单线程运行
package demo2; //import java.awt.Event; public class Demo1{ public static void main(String[] args) { Ticket t=new Ticket(); Thread p=new Thread(t); p.start(); } } class Ticket implements Runnable{ int num=1000; public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } num--; System.out.println("还剩下"+num); } } }
多线性运行:
package demo2; //import java.awt.Event; public class Demo1 { public static void main(String[] args) { Ticket t = new Ticket(); Ticket t1 = new Ticket(); Ticket t2 = new Ticket(); Thread p = new Thread(t); Thread p1 = new Thread(t1); Thread p2 = new Thread(t2); p.start(); p1.start(); p2.start(); } } class Ticket implements Runnable { int num = 1000; public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } num--; System.out.println("还剩下" + num); } } }
加上synchronized (this) 解决同步问题:
synchronized (objiect) 其中object可以任意取,保证原子性
(加锁)
package demo2; //import java.awt.Event; public class Demo1 { public static void main(String[] args) { Ticket t = new Ticket(); Thread p = new Thread(t); Thread p1 = new Thread(t); Thread p2 = new Thread(t); p.start(); p1.start(); p2.start(); } } class Ticket implements Runnable { int num = 1000; public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public void run() { // TODO Auto-generated method stub while (true) { synchronized (this) { if (num > 0) { try { Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } num--; System.out.println("还剩下" + num); } else { break; } } } } }