标签:image 提交 lang str mic down 线程状态 check inter
public class MyThread extends Thread {
public void run() {
for (int i = 0; i < 100; i++)
System.out.println(getName() + i);
}
}
// test
MyThread my1=new MyThread();//设置线程名
MyThread my2=new MyThread();
my1.setName("线程1 ");
my2.setName("线程2 ");
my1.start();
my2.start();
Thread类使用静态代理实现,Thread构造函数接收一个实现Runnable接口的类作为代理类
Thread类本身继承自Runnable接口
public class DeamonDemo implements Runnable{
@Override
public void run() {
for(int i = 0;i<100;i++)
System.out.println(Thread.currentThread().getName()+"---"+i);
}
}
// test
public class DeamonTest {
public static void main(String[] args) {
DeamonDemo d = new DeamonDemo();
Thread d1 = new Thread(d);
Thread d2 = new Thread(d);
d1.start();
d2.start();
}
}
Callable是线程池的方式创建线程,可以获取到执行函数的返回值,还可以在执行时抛出异常
public class TestCallable<T> implements Callable<T> {
@Override
public T call() throws Exception {
for (int cnt = 0; cnt < 100;cnt++) {
System.out.println(Thread.currentThread().getName()+" :cnt = " + cnt);
}
return null;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int cnt = 0; cnt < 100;cnt++) {
System.out.println(Thread.currentThread().getName()+" :cnt = " + cnt);
}
// 创建线程类
TestCallable<Object> testCallable1 = new TestCallable<>();
// 创建线程池,初始化线程池大小
ExecutorService executorService = Executors.newFixedThreadPool(3);
// 提交执行
Future<Object> future = executorService.submit(testCallable1);
// 获取执行结果
Object result = future.get();
// 服务关闭
executorService.shutdown();
}
}
DeamonDemo d = new DeamonDemo();
Thread d1 = new Thread(d);
Thread d2 = new Thread(d);
d1.setDaemon(true); // 设置守护线程
d2.setDaemon(true);
d1.start();
d2.start();
for(int i = 0;i<10;i++){
//打印main线程(主线程)线程名
System.out.println(Thread.currentThread().getName()+"---"+i);
}
PriorityDemo p = new PriorityDemo();
Thread tp1 = new Thread(p);
Thread tp2 = new Thread(p);
Thread tp3 = new Thread(p);
tp1.setName("xyg");
tp2.setName("wdf");
tp3.setName("OoO");
tp1.setPriority(10); // 最高优先级
tp2.setPriority(1);
tp3.setPriority(1);
tp1.start();
tp2.start();
tp3.start();
线程的六种状态
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
while (true) {
if (thread.getState() == Thread.State.TERMINATED) {
break;
}
System.out.println("thread = " + thread.getState());
}
}
...
thread = TIMED_WAITING
thread = TIMED_WAITING
thread = RUNNABLE
thread = RUNNABLE
thread = TERMINATED
JoinDemo p = new JoinDemo();
Thread tp1 = new Thread(p);
Thread tp2 = new Thread(p);
Thread tp3 = new Thread(p);
tp1.setName("xyg");
tp2.setName("fuck");
tp3.setName("wdnmd");
tp1.setPriority(10);
tp2.setPriority(1);
tp3.setPriority(1);
tp1.start();
try {
tp1.join(); // 其他线程等待该线程终止
} catch (InterruptedException e) {
e.printStackTrace();
}
tp2.start();
tp3.start();
等待唤醒机制:
wait()和sleep()
public synchronized void set(String name, int age) {
//如果有数据则等待
if (flag) {
try {
wait(); // 线程等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//设置值
this.name=name;
this.age=age;
// 修改标记
flag = true;
notify();// 线程唤醒
}
...
public synchronized void get(){
//如果没有数据就等待
if(!flag){
try {
wait(); // 线程等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(name+" "+age);
//修改标记
flag=false;
notify(); // 线程唤醒
}
public void run() {
for(int i = 0;i<100;i++){
System.out.println(Thread.currentThread().getName()+"---"+i);
Thread.yield(); //执行其他线程
}
}
多个线程操作同一个数据,解决:添加锁
...
public void run() {
if (x%2==0) {
//同步代码块
synchronized (this) {// 多个线程使用同一个锁对象
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票。");
}
}
}
public void run() {
check();
}
...
//同步方法
//同步方法的锁对象是this对象
//静态同步方法的锁对象是 类名.class Class类型对象
private synchronized void check() {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在出售第" + (ticket--) + "张票。");
}
}
Arraylist线程安全
public static void main(String[] args) {
ArrayList<Object> objects = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
Thread thread = new Thread(() -> {
synchronized (objects){
objects.add(Thread.currentThread().getName());
}
});
thread.start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("objects = " + objects.size());
}
Juc的线程安全集合
public class JucList {
public static void main(String[] args) {
CopyOnWriteArrayList<Object> objects = new CopyOnWriteArrayList<>();
for (int i = 0; i < 1000; i++) {
Thread thread = new Thread(() -> {
objects.add(Thread.currentThread().getName());
});
thread.start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("objects = " + objects.size());
}
}
标签:image 提交 lang str mic down 线程状态 check inter
原文地址:https://www.cnblogs.com/xiongyungang/p/12495740.html