java线程
线程的实现:
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; } } } } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步