标签:ack catch current err fun system cached pre 方法
需要覆写 Thread 的public void run()
方法
public class Test {
public static void main(String[] args) {
new MyThread("a").start();
new MyThread("b").start();
new MyThread("c").start();
new MyThread("d").start();
new Thread("e"){
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "一国两制,统一中国");
}
}
}.start();
}
}
class MyThread extends Thread {
public MyThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "一国两制,统一中国");
}
}
}
和上面一样,也需要覆写public void run()
方法
同样需要用到new Thread(),并传入一个Runnable对象,还可以再传入一个线程名称。
public class Main {
public static void main(String[] args) {
new Thread(new MyThread(), "a").start();
new Thread(new MyThread(), "b").start();
new Thread(new MyThread(), "c").start();
new Thread(new MyThread(), "d").start();
}
}
class MyThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "一国两制,统一中国");
}
}
}
虽然步骤要多一些,但是可以更加灵活的使用,可以继承其它的类,还有就是多个线路可以共享一个对象,但是考虑后面的线程安全问题。
public class Test {
public static void main(String[] args) {
new Thread("test"){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"关关雎鸠,在河之洲。");
}
}.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"桃之夭夭,灼灼其华。");
}
}
一般不使用stop()方法。
public class Test {
public static void main(String[] args) {
Thread t = new Thread("test") {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("呵呵,线程");
return;
}
System.out.println(Thread.currentThread().getName() + "关关雎鸠,在河之洲。");
}
};
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt(); // 中断
System.out.println(Thread.currentThread().getName() + "桃之夭夭,灼灼其华。");
}
}
public class Test {
public static void main(String[] args) {
Thread t = new Thread("test") {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// e.printStackTrace();
System.out.println("呵呵,线程");
return;
}
System.out.println(Thread.currentThread().getName() + "关关雎鸠,在河之洲。");
}
};
t.setDaemon(true); // 守护线程
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "桃之夭夭,灼灼其华。");
}
}
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t, "a").start();
new Thread(t, "b").start();
new Thread(t, "c").start();
new Thread(t, "d").start();
}
}
class MyThread implements Runnable {
private static int count = 10;
@Override
public void run() {
synchronized (this) { // 也可以传其它对象,相当于一个“锁”,对象相同的线程一次只能一个进入
while (count > 0) {
count--;
System.out.println(count);
}
}
}
}
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t, "a").start();
new Thread(t, "b").start();
new Thread(t, "c").start();
new Thread(t, "d").start();
}
}
class MyThread implements Runnable {
private static int count = 10;
@Override
public void run() {
haha();
}
public synchronized void haha(){
while (count > 0) {
count--;
System.out.println(count);
}
}
}
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t, "a").start();
new Thread(t, "b").start();
new Thread(t, "c").start();
new Thread(t, "d").start();
}
}
class MyThread implements Runnable {
private static int count = 10;
private Lock l = new ReentrantLock(true);// 参数为true表示公平锁 默认是false 不是公平锁
@Override
public void run() {
l.lock();
while (count > 0) {
count--;
System.out.println(count);
}
l.unlock();
}
}
public class Test {
public static void main(String[] args) {
//线程死锁
Culprit c = new Culprit();
Police p = new Police();
new MyThread(c,p).start();
c.say(p);
}
static class MyThread extends Thread{
private Culprit c;
private Police p;
MyThread(Culprit c,Police p){
this.c = c;
this.p = p;
}
@Override
public void run() {
p.say(c);
}
}
static class Culprit{
public synchronized void say(Police p){
System.out.println("罪犯:你放了我,我放了人质");
p.fun();
}
public synchronized void fun(){
System.out.println("罪犯被放了,罪犯也放了人质");
}
}
static class Police{
public synchronized void say(Culprit c){
System.out.println("警察:你放了人质,我放了你");
c.fun();
}
public synchronized void fun(){
System.out.println("警察救了人质,但是罪犯跑了");
}
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
//线程池
//创建线程
//创建任务
//执行任务
//关闭线程
//缓存线程池
//无限制长度
//任务加入后的执行流程
//1判断线程池是否存在空闲线程 2存在则使用 3不存在则创建线程并使用
//向线程池中加入新的任务
ExecutorService service = Executors.newCachedThreadPool();
//指挥线程池执行新的任务
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "锄禾日当午");
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "锄禾日当午");
}
});
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "锄禾日当午");
}
});
}
}
pool-1-thread-1锄禾日当午
pool-1-thread-1锄禾日当午
pool-1-thread-2锄禾日当午
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
/*定长线程池
长度是指定的线程池
加入任务后的执行流程
1 判断线程池是否存在空闲线程
2 存在则使用
3 不存在空闲线程 且线程池未满的情况下 则创建线程 并放入线程池中 然后使用
4 不存在空闲线程 且线程池已满的情况下 则等待线程池的空闲线程
**/
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
});
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
/*单线程线程池
执行流程
1 判断线程池的那个线程是否空闲
2 空闲则使用
3 不空闲则等待它空闲后再使用
**/
public static void main(String[] args) {
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
});
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
});
service.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
});
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Test {
/*周期任务 定长线程池
执行流程
1 判断线程池是否存在空闲线程
2 存在则使用
3 不存在空闲线程 且线程池未满的情况下 则创建线程 并放入线程池中 然后使用
4 不存在空闲线程 且线程池已满的情况下 则等待线程池的空闲线程
周期性任务执行时
定时执行 当某个任务触发时 自动执行某任务
**/
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
//定时执行一次
//参数1:定时执行的任务
//参数2:时长数字
//参数3:2的时间单位 Timeunit的常量指定
/* scheduledExecutorService.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
},5, TimeUnit.SECONDS); //5秒钟后执行*/
/*
周期性执行任务
参数1:任务
参数2:延迟时长数字(第一次在执行上面时间以后)
参数3:周期时长数字(没隔多久执行一次)
参数4:时长数字的单位
* **/
scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"锄禾日当午");
}
},5,1,TimeUnit.SECONDS);
}
}
public class Test {
public static void main(String[] args) {
//多线程通信 生产者与消费者问题
Food f = new Food();
new Cook(f).start();
new Waiter(f).start();
}
//厨师
static class Cook extends Thread{
private Food f;
public Cook(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(i%2==0){
f.setNameAndTaste("老干妈小米粥","香辣味");
}else {
f.setNameAndTaste("煎饼果子","甜辣味");
}
}
}
}
//服务员
static class Waiter extends Thread{
private Food f;
public Waiter(Food f) {
this.f = f;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
f.get();
}
}
}
//食物
static class Food{
private String name;
private String taste;
//true表示可以生产
boolean flag = true;
public synchronized void setNameAndTaste(String name,String taste){
if(flag){
this.name = name;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.taste = taste;
flag = false;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void get(){
if(!flag){
System.out.println("服务员端走的菜的名称是:"+name+",味道是:"+taste);
flag = true;
this.notifyAll();
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
当调用的对象只有一个方法时,可以用来简化调用Runnable对象的写法。
public class Test {
/*
lambda表达式
函数式编程思想
**/
public static void main(String[] args) {
//冗余的Runnable编写方式
/* Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("锄禾日当午");
}
});
t.start();*/
Thread t = new Thread(() -> System.out.println("锄禾日当午"));
t.start();
}
}
标签:ack catch current err fun system cached pre 方法
原文地址:https://www.cnblogs.com/laixiangdong/p/13872449.html