创建方法一:继承Thread方法
package com.zhan.Demo01;
//创建线程的方法一:继承Thread类,重写run()方法,调用start开启线程
//总结:注意,线程开启不一定立即执行,由CPU调度
public class TestThread1 extends Thread{
@Override
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在学习java" +i);
}
}
public static void main(String[] args) {
//main线程,主线程
//创建一个线程对象
TestThread1 testThread1 = new TestThread1();
//调用start()方法开启线程
testThread1.start();
for (int i = 0; i < 1000; i++) {
System.out.println("我在学习多线程aaaaaaaaa");
}
}
}
多线程网图下载
package com.zhan.Demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread2 extends Thread{
private String url;
private String name;
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
public static void main(String[] args) {
TestThread2 t1 = new TestThread2("https://img-home.csdnimg.cn/images/20220421094308.png","1.jpg");
TestThread2 t2 = new TestThread2("https://img-blog.csdnimg.cn/img_convert/7e5786f1a93ed9dbcc3ee271c4486a0c.png","2.jpg");
TestThread2 t3 = new TestThread2("https://img-blog.csdnimg.cn/img_convert/ef36962844784558990be3298f8486f4.png","3.jpg");
t1.start();
t2.start();
t3.start();
}
@Override
public void run() {
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为:"+name);
}
}
//下载器
class WebDownloader{
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("io异常,downloder方法出现异常");
}
}
}
创建方法二:实现rannable接口
package com.zhan.Demo01;
public class TestThread3 implements Runnable{
public void run() {
//run方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在学习java" +i);
}
}
//main线程,主线程
public static void main(String[] args) {
TestThread3 testThread3 = new TestThread3();
new Thread(testThread3).start();
for (int i = 0; i < 1000; i++) {
System.out.println("我在学习多线程aaaaaaaaa");
}
}
}
并发问题:
package com.zhan.Demo01;
public class TestThread4 implements Runnable{
private int tickNums = 15;
public void run() {
while (true){
if(tickNums<=0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"买到了第"+tickNums--+"张票");
}
}
public static void main(String[] args) {
TestThread4 testThread4 = new TestThread4();
new Thread(testThread4,"小明").start();
new Thread(testThread4,"小红").start();
new Thread(testThread4,"小黑").start();
}
}
龟兔赛跑:
package com.zhan.Demo01;
//模拟龟兔赛跑
public class Race implements Runnable{
private static String winner;
public void run() {
//判断比赛是否结束
for (int i = 0; i <=100; i++) {
if (Thread.currentThread().getName().equals("兔子")) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
boolean flag = gameOver(i);
if (flag){
break;
}
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
}
}
//判断是否完成比赛
private boolean gameOver(int steps){
//判断是否有胜利者
if (winner!=null){
return true;
}{
if (steps>=100){
winner = Thread.currentThread().getName();
System.out.println("winner is "+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
}
静态代理模式:
package com.zhan.Demo01;
//静态代理模式
//真实对象和代理对象都要实现同一个接口
//代理对象要代理真实角色
public class StaticProxy {
public static void main(String[] args) {
WeddingCompany weddingCompany = new WeddingCompany(new You());
weddingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
class You implements Marry{
public void HappyMarry() {
System.out.println("结婚了");
}
}
class WeddingCompany implements Marry{
//真实目标角色
private Marry target;
public WeddingCompany(Marry target) {
this.target = target;
}
public void HappyMarry() {
before();
this.target.HappyMarry();//这就是真实对象
after();
}
private void after() {
System.out.println("结婚之后");
}
private void before() {
System.out.println("结婚之前");
}
}
lamda表达式:
package com.zhan.Lambda;
public class TestLambda1 {
public static void main(String[] args) {
lambda like = ()->{
System.out.println("11");
};
like.sun();
}
}
interface lambda{
public void sun();
}
线程停止:
package com.zhan.Demo02;
public class TestStop implements Runnable{
private boolean flag = true;
@Override
public void run() {
int i =0;
while (flag){
System.out.println("到哪里哦饭的烦恼撒哦"+i++);
}
}
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 < 1000; i++) {
System.out.println("第"+i);
if (i==900){
testStop.stop();
System.out.println("线程停止了");
}
}
}
}
信号灯法:
利用标志位
package com.zhan.gaoji;
public class TestPc2 {
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者-->演员
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("aaaaaaaaaaaaaa");
}else {
this.tv.play("bbbbbbbbbbbbbb");
}
}
}
}
//消费者-->观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv=tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.tv.watch();
}
}
}
//产品-->节目
class TV{
//演员表演,观众等待
String voice;
boolean flag=true;
//表演
public synchronized void play(String voice){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了"+voice);
//通知观众观看
this.notifyAll();//通知唤醒
this.voice =voice;
this.flag = !this.flag;
}
//观看
public synchronized void watch(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观看了"+voice);
//通知演员表演
this.notifyAll();
this.flag = !this.flag;
}
}