Thread
//继承Thread类,重写run方法,调用start开启线程
//注意:线程开启不一定立即执行,由CPU调度执行
public class Demo01 extends Thread{
//
public void run(){
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码==>"+i);
}
}
//main主线程
public static void main(String[] args) {
//创建一个线程对象
Demo01 demo01 = new Demo01();
//使用start方法调用线程
demo01.start();
// demo01.run();
for (int i = 0; i < 500; i++) {
System.out.println("我是主线程==>"+i);
}
}
}
用多线程下载图片
public class TestThread2 extends Thread{
private String url;
private String name;
public TestThread2(String url,String name){
this.name=name;
this.url=url;
}
@Override
public void run() {
webDownloader webDownloader=new webDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名为"+name);
}
public static void main(String[] args) {
TestThread2 testThread2 = new TestThread2("https://ts1.cn.mm.bing.net/th/id/R-C.0c757425c6e862321a280837dda08790?rik=BH0qYpmCKEQsZA&riu=http%3a%2f%2fwww.jzrb.com%2ftxy%2fupload%2f2013331102535.JPG&ehk=%2fzW3AETujwc1zgsFpavomYx88LhXWN7ZF7qlyebkoxg%3d&risl=&pid=ImgRaw&r=0","1.jpg");
testThread2.start();
}
}
class webDownloader{
public void downloader(String url,String name){
try {
//下载图片的工具类FileUtils.copyURLToFile
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
System.out.println("io异常,方法出现问题");
}
}
}
Runnable
//创建线程方式2,实现runnable接口,重写run方法
public class TestRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我是子线程!!!"+i);
}
}
public static void main(String[] args) {
//创建runnable接口的实现类对象
TestRunnable testRunnable = new TestRunnable();
//创建线程对象,通过线程对象来开启我们的线程,代理
new Thread(testRunnable).start();
for (int i = 0; i < 500; i++) {
System.out.println("我是主线程"+i);
}
}
}
龟兔赛跑
Thread.sleep 让线程睡眠
public class Race implements Runnable{
private static String winner;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
if (Thread.currentThread().getName().equals("兔子")&&i%10==0){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
boolean f=gameOver(i);
if (f){break;}
System.out.println(Thread.currentThread().getName()+"=====》跑了"+i+"步");
}
}
public 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();
}
}
静态代理模式
public class DaoLi {
/*
真实对象和代理对象都要实现同一个接口
代理对象要代理真实角色
代理的好处:代理对象可以左很多真实对象做不了的事情,真实对象可以专注做自己的事情
* */
public static void main(String[] args) {
new Hqgs(new You()).HappyMarry();
new Thread(()-> System.out.println("你好呀")).start();
}
}
//结婚
interface Marry{
void HappyMarry();
}
//我
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("结婚啦,真开心");
}
}
//代理类
class Hqgs implements Marry{
private You you;
public Hqgs(You you) {
this.you = you;
}
@Override
public void HappyMarry() {
HunQian();
you.HappyMarry();
HunHou();
}
private void HunHou() {
System.out.println("婚后,收尾款");
}
private void HunQian() {
System.out.println("婚前,布置现场");
}
}
性能的优先级
//默认优先级都是相同
//设置优先级
TestRunnable testRunnable = new TestRunnable();
Thread t1=new Thread(testRunnable);
ti.setPriority(10); //10是最高 1是最小 优先级高的就先运行
守护线程
线程分为用户线程和守护线程
虚拟机必须保证用户线程执行完毕,但不用等待守护线程执行完毕
.setDeamon
public class TestDeamon {
public static void main(String[] args) {
God god=new God();
You1 you1=new You1();
Thread thread=new Thread(god);
thread.setDaemon(true); //设置成为守护线程
thread.start();
new Thread(you1).start();
}
}
class You1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 36500; i++) {
System.out.println("开心的活了每一天===");
}
System.out.println("goodbey world!==============");
}
}
class God implements Runnable{
@Override
public void run() {
while (true){
System.out.println("上帝守护着你");
}
}
}
标签:Thread,void,线程,println,new,多线程,public From: https://www.cnblogs.com/wukangxiang/p/17284492.html