多线程(一)
多线程的创建
方式一 继承于Thread类
1,创建一个继承于Thread类的子类
2.重写Thread类的run()
3.创建Thread类的子类的对象
4.通过此对象调用start()
方式二 :实现Runnable接口
1,创建一个实现了Runnable接口的类
2,实现类去实现Runnable中得抽象方法run();
3,创建实现类的对象
4,将次对象作为参数传递到Thread类的构造器中,创建Thread类得对象
5,通过Thread类大的对象调用start()
比较创建线程的两种方式:
开发中 : 优先选择 :实现Runnable接口的方式
原因: 1.实现的方式没有类的单继承性的局限性
2.实现的方式更适合来处理多个线程的共享数据的情况
联系: public class Thread implements Runnable
相同点 : 两种方式都需要重写run(),将线程要执行的逻辑声明在run()中
方式三 :实现Callable 接口
1.创建一个实现callable的实现类
2.重写call 方法,将此线程需要执行的操作 声明在方法call()中
3.创建callable接口实现类的对象
4.将此callable接口实现类的对象传递到FutureTask构造器中,创建futureTask对象
5.将FutureTask的对象作为参数传递到Thread类的构造器中,创建Thread对象,并调用start()
多线程练习
package com. kuang. Demo01;
public class TestThread1 extends Thread {
@Override
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( "看代码" ) ;
}
}
public static void main ( String[ ] args) {
TestThread1 testThread1 = new TestThread1 ( ) ;
testThread1. start ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( "主线程" ) ;
}
}
}
package com. kuang. 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;
}
@Override
public void run ( ) {
WebDownLoader webDownLoader = new WebDownLoader ( ) ;
webDownLoader. downloader ( url, name) ;
System. out. println ( "下载了文件名 : " + name) ;
}
public static void main ( String[ ] args) {
TestThread2 t1 = new TestThread2 ( "https://pic.netbian.com/uploads/allimg/210328/230405-1616943845e25f.jpg" , "1.jpg" ) ;
TestThread2 t2 = new TestThread2 ( "https://pic.netbian.com/uploads/allimg/210328/225553-1616943353aca5.jpg" , "2.jpg" ) ;
TestThread2 t3 = new TestThread2 ( "https://pic.netbian.com/uploads/allimg/210328/210356-161693663629a0.jpg" , "3.jpg" ) ;
t1. start ( ) ;
t2. start ( ) ;
t3. start ( ) ;
}
}
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异常,downloader 方法出现问题" ) ;
}
}
}
package com. kuang. Demo01;
public class TestThread3 implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( "看代码" ) ;
}
}
public static void main ( String[ ] args) {
TestThread3 testThread3 = new TestThread3 ( ) ;
new Thread ( testThread3) . start ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System. out. println ( "主线程" ) ;
}
}
}
package com. kuang. Demo01;
public class TestThread4 implements Runnable {
private int ticketsNumber = 10 ;
@Override
public void run ( ) {
while ( true ) {
if ( ticketsNumber <= 0 ) {
break ;
}
try {
Thread. sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + "----->拿了第" + ticketsNumber + "票" ) ;
}
}
public static void main ( String[ ] args) {
TestThread4 ticket = new TestThread4 ( ) ;
new Thread ( ticket, "小明" ) . start ( ) ;
new Thread ( ticket, "小红" ) . start ( ) ;
new Thread ( ticket, "小绿" ) . start ( ) ;
new Thread ( ticket, "黄牛" ) . start ( ) ;
new Thread ( ticket, "黄牛" ) . start ( ) ;
}
}
Lambda
package com. kuang. lambda;
public class TestLambda1 {
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 ( ) ;
class Like3 implements ILIke {
@Override
public void lambda ( ) {
System. out. println ( "I like lambda3" ) ;
}
}
like = new Like3 ( ) ;
like. lambda ( ) ;
like = new ILIke ( ) {
@Override
public void lambda ( ) {
System. out. println ( "I like lambda4" ) ;
}
} ;
like. lambda ( ) ;
like = ( ) - > { System. out. println ( "I like lambda2" ) ; } ;
like. lambda ( ) ;
}
}
interface ILIke {
void lambda ( ) ;
}
class Like implements ILIke {
@Override
public void lambda ( ) {
System. out. println ( "I like lambda" ) ;
}
}
package com. kuang. lambda;
public class TestLambda2 {
public static void main ( String[ ] args) {
ILove love = null;
love = ( a, b) - > {
System. out. println ( " I love you ---->" + a) ;
System. out. println ( " I love you ---->" + b) ;
} ;
love. love ( 521 , 502 ) ;
}
}
interface ILove {
void love ( int a, int b) ;
}
多线程方法
package com. kuang. state;
public class TestDaemon {
public static void main ( String[ ] args) {
God god = new God ( ) ;
You you = new You ( ) ;
Thread thread = new Thread ( god) ;
thread. setDaemon ( true ) ;
thread. start ( ) ;
new Thread ( you) . start ( ) ;
}
}
class God implements Runnable {
@Override
public void run ( ) {
while ( true ) {
System. out. println ( "上帝保佑你" ) ;
}
}
}
class You implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 36500 ; i++ ) {
System. out. println ( "你一生都开心的活着" ) ;
}
System. out. println ( " goodbye World " ) ;
}
}
package com. kuang. state;
public class TestJoin implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 1000 ; i++ ) {
System. out. println ( "VIP 来了" + i) ;
}
}
public static void main ( String[ ] args) throws InterruptedException {
TestJoin testJoin = new TestJoin ( ) ;
Thread thread = new Thread ( testJoin) ;
thread. start ( ) ;
for ( int i = 0 ; i < 500 ; i++ ) {
if ( i== 200 ) {
thread. join ( ) ;
}
System. out. println ( "main" + i ) ;
}
}
}
package com. kuang. state;
public class TestPriority {
public static void main ( String[ ] args) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "--->" + Thread. currentThread ( ) . getPriority ( ) ) ;
MyPriority myPriority = new MyPriority ( ) ;
Thread t1 = new Thread ( myPriority) ;
Thread t2 = new Thread ( myPriority) ;
Thread t3 = new Thread ( myPriority) ;
Thread t4 = new Thread ( myPriority) ;
Thread t5 = new Thread ( myPriority) ;
Thread t6 = new Thread ( myPriority) ;
t1. start ( ) ;
t2. setPriority ( 1 ) ;
t2. start ( ) ;
t3. start ( ) ;
t4. setPriority ( 4 ) ;
t4. start ( ) ;
t5. setPriority ( Thread. MAX_PRIORITY) ;
t5. start ( ) ;
t6. setPriority ( 7 ) ;
t6. start ( ) ;
}
}
class MyPriority implements Runnable {
@Override
public void run ( ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "--->" + Thread. currentThread ( ) . getPriority ( ) ) ;
}
}
package com. kuang. state;
import com. kuang. Demo01. TestThread4;
public class TestSleep implements Runnable {
private int ticketsNumber = 10 ;
@Override
public void run ( ) {
while ( true ) {
if ( ticketsNumber <= 0 ) {
break ;
}
try {
Thread. sleep ( 100 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + "----->拿了第" + ticketsNumber + "票" ) ;
}
}
public static void main ( String[ ] args) {
TestThread4 ticket = new TestThread4 ( ) ;
new Thread ( ticket, "小明" ) . start ( ) ;
new Thread ( ticket, "小红" ) . start ( ) ;
new Thread ( ticket, "小绿" ) . start ( ) ;
new Thread ( ticket, "黄牛" ) . start ( ) ;
new Thread ( ticket, "黄牛" ) . start ( ) ;
}
}
package com. kuang. state;
import java. text. SimpleDateFormat;
import java. util. Date;
public class TestSleep2 {
public static void main ( String[ ] args) {
Date startTime = new Date ( System. currentTimeMillis ( ) ) ;
while ( true ) {
try {
Thread. sleep ( 1000 ) ;
System. out. println ( new SimpleDateFormat ( "HH:mm:ss" ) . format ( startTime) ) ;
startTime = new Date ( System. currentTimeMillis ( ) ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public static void tenDown ( ) throws InterruptedException{
int num = 10 ;
while ( true ) {
Thread. sleep ( 100 ) ;
System. out. println ( num-- ) ;
if ( num < 0 ) {
break ;
}
}
}
}
package com. kuang. state;
public class TestState {
public static void main ( String[ ] args) throws InterruptedException {
Thread thread = new Thread ( ( ) - > {
for ( int i = 0 ; i < 5 ; i++ ) {
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
System. out. println ( "" ) ;
} ) ;
Thread. State state = thread. getState ( ) ;
System. out. println ( state) ;
thread. start ( ) ;
state = thread. getState ( ) ;
System. out. println ( state) ;
while ( state != Thread. State. TERMINATED) {
Thread. sleep ( 1000 ) ;
state = thread. getState ( ) ;
System. out. println ( state) ;
}
}
}
package com. kuang. state;
public class TestStop implements Runnable {
private boolean flag = true ;
@Override
public void run ( ) {
int i = 0 ;
while ( flag) {
System. out. println ( "run......Thread" + 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 ( "main" + i) ;
if ( i == 900 ) {
testStop. stop ( ) ;
System. out. println ( "线程该停止了" ) ;
}
}
}
}
package com. kuang. state;
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 ( ) + "线程开始执行" ) ;
Thread. yield ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "线程停止执行" ) ;
}
}
线程不安全示例
package com. kuang. syn;
public class UnsafeBank {
public static void main ( String[ ] args) {
Account account = new Account ( 100 , "基金" ) ;
Drawing you = new Drawing ( account, 50 , "你" ) ;
Drawing girlFriend= new Drawing ( account, 100 , "girlFriend" ) ;
you. start ( ) ;
girlFriend. start ( ) ;
}
}
class Account {
int money;
String name;
public Account ( int money , String name) {
this . money = money;
this . name = name;
}
}
class Drawing extends Thread {
Account acct;
int drawingMoney;
int nowMoney;
public Drawing ( Account acct, int drawingMoney, String name) {
super ( name) ;
this . acct = acct;
this . drawingMoney = drawingMoney;
}
@Override
public void run ( ) {
if ( acct. money - drawingMoney < 0 ) {
System. out. println ( "钱不够,取不了" ) ;
return ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
acct. money = acct. money - drawingMoney;
nowMoney = nowMoney + drawingMoney;
System. out. println ( acct. name + "余额为:" + acct. money) ;
System. out. println ( this . getName ( ) + "手里的钱 : " + nowMoney) ;
}
}
package com. kuang. syn;
public class UnsafeBuyticket {
public static void main ( String[ ] args) {
Buyticket buyticket = new Buyticket ( ) ;
new Thread ( buyticket, "我" ) . start ( ) ;
new Thread ( buyticket, "你" ) . start ( ) ;
new Thread ( buyticket, "黄牛" ) . start ( ) ;
}
}
class Buyticket implements Runnable {
private int ticketNum = 10 ;
boolean flag = true ;
@Override
public void run ( ) {
while ( flag) {
try {
buy ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public void buy ( ) throws InterruptedException {
if ( ticketNum <= 0 ) {
return ;
}
Thread. sleep ( 100 ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "拿到" + ticketNum-- ) ;
}
}
package com. kuang. syn;
import java. util. ArrayList;
public class UnsafeList {
public static void main ( String[ ] args) {
ArrayList< String> list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
new Thread ( ( ) - > {
list. add ( Thread. currentThread ( ) . getName ( ) ) ;
} ) . start ( ) ;
}
try {
Thread. sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( list. size ( ) ) ;
}
}