第一篇 多线程
Posted zhangzhipeng001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一篇 多线程相关的知识,希望对你有一定的参考价值。
1 package com.zzp.thread; 2 /** 3 * 4 * 创建线程:方式一 5 * 1、继承thread类,重写run方法 6 * 2、启动线程,调用start方法 7 * 8 * @author java 9 * 10 */ 11 public class StartThread extends Thread{ 12 //重写run() 13 @Override 14 public void run() { 15 for(int i=0;i<10;i++){ 16 System.out.println("一边学习"); 17 } 18 } 19 20 public static void main(String[] args) { 21 //创建Thread子类对象 22 StartThread st = new StartThread(); 23 //启动,并不是立即启动,而是等待cpu调用,至于CPU何时调用,不知道 24 //开启线程的时机很重要 25 st.start(); 26 //如果直接调用run()方法,则是普通方法调用,不是开启多线程 27 //st.run(); 28 29 for(int i=0;i<10;i++){ 30 System.out.println("一边听歌"); 31 } 32 } 33 }
1 package com.zzp.thread; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 8 import org.apache.commons.io.FileUtils; 9 10 /** 11 * 12 * 下载工具类 13 * @author java 14 * 15 */ 16 public class WebDownLoader { 17 public void downLoader(String url,String name){ 18 try { 19 FileUtils.copyURLToFile(new URL(url), new File(name)); 20 } catch (MalformedURLException e) { 21 e.printStackTrace(); 22 } catch (IOException e) { 23 e.printStackTrace(); 24 } 25 } 26 }
1 package com.zzp.thread; 2 /** 3 * 4 * 创建图片下载的线程 5 * @author java 6 * 7 */ 8 public class TDownLoader extends Thread{ 9 private String url; 10 private String name; 11 12 public TDownLoader(String url, String name) { 13 super(); 14 this.url = url; 15 this.name = name; 16 } 17 //重写run() 18 @Override 19 public void run() { 20 WebDownLoader wd = new WebDownLoader(); 21 wd.downLoader(url, name); 22 } 23 24 public static void main(String[] args) { 25 TDownLoader td1 = new TDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%AE%88%E7%A0%B4%E7%A6%BB&step_word=&hs=0&pn=1&spn=0&di=79296871830&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1435309020%2C3477682663&os=1841078546%2C3614953869&simid=4194686237%2C632091804&adpicid=0&lpn=0&ln=104&fr=&fmq=1536156608183_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Ft2.qpic.cn%2Fmblogpic%2Fee290b3b55e018eb8fac%2F2000&fromurl=ippr_z2C%24qAzdH3FAzdH3Fr_z%26e3Bp_z%26e3Bqq_z%26e3Bv54AzdH3Fs5g2ojtk5AzdH3Frw2j_z%26e3Brir%3Ft1%3Dn8bdcba9acn0cl9%26st1%3D8b989dd8mbcdalmcl8db&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","spl.jpg"); 26 TDownLoader td2 = new TDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E6%9D%8E%E5%B0%8F%E9%BE%99&step_word=&hs=0&pn=0&spn=0&di=94581897410&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=2752933258%2C795342213&os=1992076552%2C3728441564&simid=3465896125%2C219929152&adpicid=0&lpn=0&ln=3370&fr=&fmq=1536156674237_R&fm=result&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=star&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimgcache.cjmx.com%2Fstar%2F201607%2F20160719231717732.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Ffpw6_z%26e3Bv34x_z%26e3Bv54AzdH3Fda8ma0AzdH3F8abdba8_z%26e3Bfip4s&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","lxl.jpg"); 27 TDownLoader td3 = new TDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E6%B2%B3%E5%8D%97%E7%90%86%E5%B7%A5%E5%A4%A7%E5%AD%A6&step_word=&hs=0&pn=0&spn=0&di=115015699590&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=3620107849%2C3756859208&os=1374852277%2C1275983326&simid=4171483572%2C611783800&adpicid=0&lpn=0&ln=1851&fr=&fmq=1536156735988_R&fm=result&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimg.gaosan.com%2Fupload%2Fwebimg%2F1785.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B2w5fwg_z%26e3Bv54AzdH3F2w5hw5AzdH3F88d0la_z%26e3Bip4s&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","lgd.jpg"); 28 29 td1.start(); 30 td2.start(); 31 td3.start(); 32 } 33 34 }
1 package com.zzp.thread; 2 /** 3 * 4 * 创建线程:方式二 5 * 1、实现runnable接口,重写run方法 6 * 2、启动线程+thread类+调用start方法 7 * 推荐使用,避免单继承造成的局限性 8 * @author java 9 * 10 */ 11 public class StartRun implements Runnable{ 12 //重写run() 13 @Override 14 public void run() { 15 for(int i=0;i<10;i++){ 16 System.out.println("一边学习"); 17 } 18 } 19 20 public static void main(String[] args) { 21 //创建Thread子类对象 22 //StartRun sr = new StartRun(); 23 //创建Thread类对象 24 //Thread t = new Thread(sr); 25 //启动,并不是立即启动,而是等待cpu调用,至于CPU何时调用,不知道 26 //开启线程的时机很重要 27 //t.start(); 28 //如果直接调用run()方法,则是普通方法调用,不是开启多线程 29 //st.run(); 30 31 //如果创建的对象只用一次 32 new Thread(new StartRun()).start(); 33 34 for(int i=0;i<10;i++){ 35 System.out.println("一边听歌"); 36 } 37 } 38 }
1 package com.zzp.thread; 2 /** 3 * 4 * 创建图片下载的线程 5 * @author java 6 * 7 */ 8 public class IDownLoader implements Runnable{ 9 private String url; 10 private String name; 11 12 public IDownLoader(String url, String name) { 13 super(); 14 this.url = url; 15 this.name = name; 16 } 17 //重写run() 18 @Override 19 public void run() { 20 WebDownLoader wd = new WebDownLoader(); 21 wd.downLoader(url, name); 22 } 23 24 public static void main(String[] args) { 25 IDownLoader td1 = new IDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E5%AE%88%E7%A0%B4%E7%A6%BB&step_word=&hs=0&pn=1&spn=0&di=79296871830&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1435309020%2C3477682663&os=1841078546%2C3614953869&simid=4194686237%2C632091804&adpicid=0&lpn=0&ln=104&fr=&fmq=1536156608183_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Ft2.qpic.cn%2Fmblogpic%2Fee290b3b55e018eb8fac%2F2000&fromurl=ippr_z2C%24qAzdH3FAzdH3Fr_z%26e3Bp_z%26e3Bqq_z%26e3Bv54AzdH3Fs5g2ojtk5AzdH3Frw2j_z%26e3Brir%3Ft1%3Dn8bdcba9acn0cl9%26st1%3D8b989dd8mbcdalmcl8db&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","spl.jpg"); 26 IDownLoader td2 = new IDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E6%9D%8E%E5%B0%8F%E9%BE%99&step_word=&hs=0&pn=0&spn=0&di=94581897410&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=2752933258%2C795342213&os=1992076552%2C3728441564&simid=3465896125%2C219929152&adpicid=0&lpn=0&ln=3370&fr=&fmq=1536156674237_R&fm=result&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=star&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimgcache.cjmx.com%2Fstar%2F201607%2F20160719231717732.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Ffpw6_z%26e3Bv34x_z%26e3Bv54AzdH3Fda8ma0AzdH3F8abdba8_z%26e3Bfip4s&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","lxl.jpg"); 27 IDownLoader td3 = new IDownLoader("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E6%B2%B3%E5%8D%97%E7%90%86%E5%B7%A5%E5%A4%A7%E5%AD%A6&step_word=&hs=0&pn=0&spn=0&di=115015699590&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=3620107849%2C3756859208&os=1374852277%2C1275983326&simid=4171483572%2C611783800&adpicid=0&lpn=0&ln=1851&fr=&fmq=1536156735988_R&fm=result&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fimg.gaosan.com%2Fupload%2Fwebimg%2F1785.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B2w5fwg_z%26e3Bv54AzdH3F2w5hw5AzdH3F88d0la_z%26e3Bip4s&gsm=0&rpstart=0&rpnum=0&islist=&querylist=","lgd.jpg"); 28 29 new Thread(td1).start(); 30 new Thread(td2).start(); 31 new Thread(td3).start(); 32 } 33 34 }
1 package com.zzp.thread; 2 /** 3 * 4 * 共享资源,会出现并发问题(后面要确保线程安全) 5 * @author java 6 * 7 */ 8 public class Web12306 implements Runnable{ 9 private int tikectsNum = 99; 10 @Override 11 public void run() { 12 while(true){ 13 if(tikectsNum < 0){ 14 break; 15 } 16 //当设置线程延迟的时候,就会出现线程不安全的情况 17 try { 18 Thread.sleep(200); 19 } catch (InterruptedException e) { 20 e.printStackTrace(); 21 } 22 System.out.println(Thread.currentThread().getName()+"--->"+tikectsNum--); 23 } 24 } 25 public static void main(String[] args) { 26 //一份资源 27 Web12306 wb = new Web12306(); 28 //多份代理 29 new Thread(wb,"码农").start(); 30 new Thread(wb,"马云").start(); 31 new Thread(wb,"麻黄").start(); 32 } 33 }
1 package com.zzp.thread; 2 /** 3 * 4 * 模拟龟兔赛跑 5 * @author java 6 * 7 */ 8 public class Racer implements Runnable{ 9 private static String winner; 10 @Override 11 public void run() { 12 for(int i=1;i<=100;i++){ 13 //兔子每走10步,休息一次 14 /*if(Thread.currentThread().getName().equals("兔子") && (i%10 == 0)){ 15 try { 16 Thread.sleep(200); 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 }*/ 21 System.out.println(Thread.currentThread().getName()+"-->"+i); 22 boolean flag = gameOver(i); 23 if(flag){ 24 break; 25 } 26 } 27 28 } 29 public boolean gameOver(int i){ 30 if(winner != null){ 31 return true; 32 }else{ 33 if(i==100){ 34 winner = Thread.currentThread().getName(); 35 System.out.println("winner:"+winner); 36 return true; 37 } 38 } 39 return false; 40 } 41 42 public static void main(String[] args) { 43 Racer rc = new Racer(); 44 new Thread(rc,"乌龟").start(); 45 new Thread(rc,"兔子").start(); 46 } 47 }
以上是关于第一篇 多线程的主要内容,如果未能解决你的问题,请参考以下文章
C++11多线程第一篇:并发基本概念及实现,进程线程基本概念
C++11多线程第一篇:并发基本概念及实现,进程线程基本概念