> ```
> PrimeRun p = new PrimeRun(143);
> new Thread(p).start();
> ```
### **方式一**
```java
//創(chuàng)建線程方式一:繼承Thread類,重寫(xiě)run()方法,調(diào)用start開(kāi)啟線程
public class One extends Thread{
@Override
public void run(){
//run方法線程體
for (int i = 0; i < 10; i++) {
System.out.println("數(shù)字---" + i);
}
}
public static void main(String[] args) {
//main線程,主線程
//創(chuàng)建一個(gè)線程對(duì)象
One one = new One();
//調(diào)用start()方法開(kāi)啟線程
one.start();
for (int i = 0; i < 1000; i++) {
System.out.println("線程---" + i);
}
}
}
```
### **方式二**
```java
//創(chuàng)建線程方式二:實(shí)現(xiàn)runnable接口,重寫(xiě)run方法,執(zhí)行線程需要丟入runnable接口實(shí)現(xiàn)類,調(diào)用start方法。
public class Two implements Runnable{
@Override
public void run() {
//run方法線程體
for (int i = 0; i < 100; i++) {
System.out.println("數(shù)字---" + i);
}
}
public static void main(String[] args) {
//創(chuàng)建runnable接口的實(shí)現(xiàn)類對(duì)象
Two two = new Two();
//創(chuàng)建線程對(duì)象,通過(guò)線程對(duì)象來(lái)開(kāi)啟我們的線程,代理
// Thread thread = new Thread(two);
// thread.start();
new Thread(two).start();
for (int i = 0; i < 100; i++) {
System.out.println("線程--" + i);
}
}
}
```
## 并發(fā)任務(wù)
初識(shí)并發(fā):
```java
public class Ticket implements Runnable{
//票數(shù)
private int tickNums = 10;
@Override
public void run() {
while (true){
if (tickNums<=0){
break;
}
//模擬延遲
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"兔子").start();
new Thread(race,"烏龜").start();
}
}
```
## 靜態(tài)代理模式
```java
public class StaticType {
public static void main(String[] args) {
WedingCompany wedingCompany = new WedingCompany(new You());
wedingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真實(shí)對(duì)象,你去結(jié)婚
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("結(jié)婚了,超級(jí)開(kāi)心!");
}
}
//代理角色,幫助你結(jié)婚
class WedingCompany implements Marry{
```java
/*
推導(dǎo)Lambda表達(dá)式
*/
public class ClassLambda {
//3.靜態(tài)內(nèi)部類
static class Like2 implements ILike {
@Override
public void lambda() {
System.out.println("I like lambda2!");
}
}
public static void main(String[] args) {
ILike like = new Like();
like.lambda();
like = new Like2();
like.lambda();
//4.局部?jī)?nèi)部類
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("I like lambda3!");
}
}
like = new Like3();
like.lambda();
//5.匿名內(nèi)部類,沒(méi)有類的名稱,必須借助接口或者父類
like = new ILike() {
@Override
public void lambda() {
System.out.println("I like lambda4!");
}
};
like.lambda();
//6.用lambda簡(jiǎn)化
like = ()->{
System.out.println("I like lambda5!");
};
like.lambda();
}
}
```java
//測(cè)試stop
//1.建議線程正常停止--->利用次數(shù),不建議死循環(huán)
//2.建議使用標(biāo)志位--->設(shè)置一個(gè)標(biāo)志位
//3.不要使用stop或者destroy等過(guò)時(shí)或者JDK不建議使用的方法
public class TestStop implements Runnable {
//1.設(shè)置一個(gè)標(biāo)志位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("run Thread..." + i++);
}
}
//2.設(shè)置一個(gè)公開(kāi)的方法停止線程,轉(zhuǎn)換標(biāo)志位
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 100; i++) {
System.out.println("main" + i);
if (i == 90){
//調(diào)用stop方法切換標(biāo)志位,讓線程停止
testStop.stop();
System.out.println("線程該停止了。");
}
}
}
public static void Down() throws InterruptedException {
int number = 10;
while (true) {
Thread.sleep(1000);
System.out.println(number--);
if (number <= 0)
break;
}
}
}
```
//打印當(dāng)前系統(tǒng)時(shí)間
public class SystemTime {
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis()); //獲取當(dāng)前時(shí)間
```java
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"a").start();
new Thread(myYield,"b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->start");
Thread.yield();
System.out.println(Thread.currentThread().getName() + "-->end");
}
}
```
## Join方法:線程強(qiáng)制執(zhí)行
- Join合并線程,待線程執(zhí)行完成后,在執(zhí)行其他線程,其他線程阻塞
- 不建議使用,容易導(dǎo)致進(jìn)程阻塞
```java
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("VIP插隊(duì)" + i);
}
}
public static void main(String[] args) throws InterruptedException {
//啟動(dòng)我們的線程
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
//主線程
for (int i = 0; i < 100; i++) {
if (i == 10){
thread.join();
}
System.out.println("main" + i);
}
}
}
```
public class TestLock {
public static void main(String[] args) {
wantTicket buy = new wantTicket();
new Thread(buy,"a").start();
new Thread(buy,"b").start();
new Thread(buy,"c").start();
}
}
class wantTicket implements Runnable{
int TicketNum = 10;
private final ReentrantLock lock = new ReentrantLock();
//生產(chǎn)者,消費(fèi)者,產(chǎn)品,緩沖區(qū)
public class TestPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
//生產(chǎn)者
class Productor extends Thread{
SynContainer container;
public Productor(SynContainer container){
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
container.push(new Chicken(i));
System.out.println("生產(chǎn)了" +i +"只雞");
}
}
}
//消費(fèi)者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container = container;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消費(fèi)了-->" + container.pop().id + "只雞");
}
}
}
//產(chǎn)品
class Chicken{
int id;
public Chicken(int id) {
this.id = id;
}
}
//緩沖區(qū)
class SynContainer{
//需要一個(gè)容器大小
Chicken[] chickens= new Chicken[10];
//容器計(jì)數(shù)器
int count = 0;
```java
//測(cè)試生產(chǎn)者消費(fèi)者問(wèn)題2-->信號(hào)燈法,標(biāo)志位解決
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生產(chǎn)者-->演員
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0){
this.tv.play("河南衛(wèi)視");
}else {
this.tv.play("春節(jié)聯(lián)歡晚會(huì)");
}
}
}
}
//消費(fèi)者-->觀眾
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//產(chǎn)品-->節(jié)目
class TV {
//演員表演,觀眾等待 T
//觀眾觀看,演員等待 F
String voice; //表演的節(jié)目
boolean flag = true;