Process 进程 系统资源分配的单位
Thread 线程 CPU调度和执行的单位
1.继承Thread类
package demo01;
//创建线程 方式一:继承Thread类,重写run()方法,调用start开启线程
//线程开启不一定立即执行,由cpu调度
public class TestThread1 extends Thread{
@Override
public void run(){
//run 方法线程体
for (int i = 0; i < 2000; i++) {
System.out.println(i);
}
}
public static void main(String[] args){
//创建对象
TestThread1 test = new TestThread1();
//调用start方法开启线程
test.start();
//main线程,主线程
for (int i = 0; i < 2000; i++) {
System.out.println("main" + i);
}
}
}
2.网图下载
package demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.io.IOException;
//练习Thread实现多线程同步下载图片
public class TEstThread2 extends Thread{
private String url;
private String name; //保存的文件名
public TEstThread2(String url,String name){
this.url=url;
this.name=name;
}
//重写了run方法
//下载图片线程的执行体
@Override
public void run(){
WebDownloader webDownloader=new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了"+name);
}
public static void main(String[] args) {
String url = "/i/ll/?i=1e3f94eab25a42e3bcb55aafcbfc21cf.png";
TEstThread2 t1=new TEstThread2(url,"1.png");
TEstThread2 t2=new TEstThread2(url,"2.png");
TEstThread2 t3=new TEstThread2(url,"3.png");
//调用start方法开启线程
t1.start();
t2.start();
t3.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
//把一个url变成一个文件
FileUtils.copyURLToFile(new URL(url),new File(name));
}catch (IOException e){
e.printStackTrace();
System.out.println("下载失败");
}
}
}
3.Runnable接口
package demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.io.IOException;
//练习Thread实现多线程同步下载图片
public class TEstThread2 extends Thread{
private String url;
private String name; //保存的文件名
public TEstThread2(String url,String name){
this.url=url;
this.name=name;
}
//重写了run方法
//下载图片线程的执行体
@Override
public void run(){
WebDownloader webDownloader=new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了"+name);
}
public static void main(String[] args) {
String url = "/i/ll/?i=1e3f94eab25a42e3bcb55aafcbfc21cf.png";
TEstThread2 t1=new TEstThread2(url,"1.png");
TEstThread2 t2=new TEstThread2(url,"2.png");
TEstThread2 t3=new TEstThread2(url,"3.png");
//调用start方法开启线程
t1.start();
t2.start();
t3.start();
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
//把一个url变成一个文件
FileUtils.copyURLToFile(new URL(url),new File(name));
}catch (IOException e){
e.printStackTrace();
System.out.println("下载失败");
}
}
}
package demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.io.IOException;
//练习Thread实现多线程同步下载图片
public class RunnableThread2 implements Runnable{
private String url;
private String name; //保存的文件名
public RunnableThread2(String url,String name){
this.url=url;
this.name=name;
}
//重写了run方法
//下载图片线程的执行体
@Override
public void run(){
WebDownloader1 webDownloader=new WebDownloader1();
webDownloader.downloader(url,name);
System.out.println("下载了"+name);
}
public static void main(String[] args) {
String url = "/i/ll/?i=1e3f94eab25a42e3bcb55aafcbfc21cf.png";
RunnableThread2 t1=new RunnableThread2(url,"1.png");
RunnableThread2 t2=new RunnableThread2(url,"2.png");
RunnableThread2 t3=new RunnableThread2(url,"3.png");
new Thread(t1).start();
new Thread(t2).start();
new Thread(t3).start();
// //调用start方法开启线程
// t1.start();
// t2.start();
// t3.start();
}
}
//下载器
class WebDownloader1{
//下载方法
public void downloader(String url,String name){
try {
//把一个url变成一个文件
FileUtils.copyURLToFile(new URL(url),new File(name));
}catch (IOException e){
e.printStackTrace();
System.out.println("下载失败");
}
}
}
4.初时并发问题
package demo01;
//多个线程同时操作同一个对象
//买火车票的例子
//发现问题 多个线程操作同一个资源的情况下,线程不安全,数据紊乱
public class TestThread4 implements Runnable {
//票数
private int ticketNums = 10;
@Override
public void run(){
while(true){
if (ticketNums <= 0){
break;
}
//模拟延时
try {
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ticketNums--);
}
}
public static void main(String[] args) {
TestThread4 ticket = new TestThread4();
new Thread(ticket,"1a").start();
new Thread(ticket,"2a").start();
new Thread(ticket,"3a").start();
}
}
5.龟兔赛跑
package demo01;
//模拟龟兔赛跑
public class Race implements Runnable {
//胜利者
private static String winner;
@Override
public void run(){
for (int i = 0; i < 1000; i++) {
//模拟兔子休息
if (Thread.currentThread().getName().equals("兔子") && i % 10 == 0)
{
try {
Thread.sleep(100);
}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;
}else {
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();
}
}
Static 代理
package demo02;
public class StaticProxy {
public static void main(String[] args) {
You you = new You();
new Thread(() -> System.out.println("")).start();
new WeddingCompany(new You()).HappyMarry();
// WeddingCompany weddingCompany = new WeddingCompany(you );
// weddingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
//真实角色,你去结婚
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("hunli");
}
}
//代理角色,帮助你结婚
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target) {
this.target = target;
}
@Override
public void HappyMarry() {
before();
this.target.HappyMarry();
after();
}
private void after() {
System.out.println("婚后");
}
private void before() {
System.out.println("婚前");
}
}
标签:Java,String,Thread,url,new,多线程,public,name
From: https://blog.csdn.net/lf_1234/article/details/137481594