多线程
下载图片
package com.atuo.less03;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
public class ThreadDemo1 extends Thread {
private String url;
private String name;
public ThreadDemo1(String url, String name) {
this.url = url;
this.name = name;
}
Runnable接口
package com.atuo.less03;
public class Thread3 implements Runnable {
private String name;
private String sex;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
初识并发问题
package com.atuo.less03;
public class Thread4 implements Runnable {
int ticketNums = 10;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
静态代理
package com.atuo.less01;
public class StatisPoxy {
public static void main(String[] args) {
You you = new You();
WeddingCompany weddingCompany = new WeddingCompany(you);
weddingCompany.HapperMarry();
}
}
interface Marry{
void HapperMarry();
}
//一个真实的你
class You implements Marry{
lamda表达式
package com.atuo.less02;
public class TestLamda {
public static void main(String[] args) {
Ilove love = () -> System.out.println("hello");
love.Love();
}
}
interface Ilove{
void Love();
}
抽象类中只能有一个构造方法
线程停止
package com.atuo.less02;
public class TestStop implements Runnable {
Boolean flag = true;
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
线程休眠
package com.atuo.less02;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
public class TestSleep {
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();
}
}
}
}
观察线程的状态
package com.atuo.less02;
//观察线程的状态
public class ThreadState {
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);// NEW
thread.start();
state = thread.getState();
System.out.println(state);//Run
while ( state != Thread.State.TERMINATED){
state = thread.getState();
Thread.sleep(100);
System.out.println(state);
}
}
}
优先级
package com.atuo.less02;
public class ThreadProiority {
public static void main(String[] args) {
Test test = new Test();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
Thread t3 = new Thread(test);
Thread t4 = new Thread(test);
t1.setPriority(1);
t1.start();
t2.setPriority(7);
t2.start();
t3.setPriority(4);
t3.start();
t4.setPriority(9);
t4.start();
}
}
class Test implements Runnable{
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
买票
package com.atuo.less02;
public class BuyTicket implements Runnable{
int TickNums = 10;
Boolean flag = true;
//不安全买票
public static void main(String[] args) {
BuyTicket buyTicket = new BuyTicket();
Thread t1 = new Thread(buyTicket,"苦逼的我");
Thread t2 = new Thread(buyTicket,"牛逼的我");
Thread t3 = new Thread(buyTicket,"可恶的黄牛党");
t1.start();
t2.start();
t3.start();
}
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
管程法
package com.atuo.less03;
public class TestPC{
public static void main(String[] args) {
Cache cache = new Cache();
Producer producer = new Producer(cache);
producer.start();
Consumer consumer = new Consumer(cache);
consumer.start();
}
}
class Producer extends Thread{
Cache cache;
public Producer(Cache cache){
this.cache = cache;
}