线程的命名
public class MyThread {
public static void main(String[] args) {
Runnable runnable = ()->{
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
};
Thread thread = new Thread(runnable,"thread1");
thread.start();
for (int i = 0; i < 3; i++) {
System.out.println("main"+i);
}
}
}
线程休眠
public class MyThread {
public static void main(String[] args) {
Runnable runnable = ()->{
//线程休眠
//1.参数:毫秒为单位的时间差
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
};
Thread thread = new Thread(runnable,"thread1");
thread.start();
for (int i = 0; i < 100; i++) {
System.out.println("main"+i);
}
}
}
线程的优先级
修改这个线程可以去抢到CPU时间片段的概率,其范围为[0,10],默认值为5,值越大,表明线程抢到的概率就越高。要放到线程开始执行之前设置。
public static void main(String[] args) {
Runnable runnable = ()->{
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
}
};
Thread thread = new Thread(runnable,"thread1");
Thread thread2 = new Thread(runnable, "thread2");
thread.setPriority(1);
thread2.setPriority(9);
thread.start();
thread2.start();
}
线程的礼让
指的是让当前的运行状态的线程释放自己的CPU资源,由运行状态,回到就绪状态。
public class MyThread {
public static void main(String[] args) {
Runnable runnable = ()->{
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"---->"+i);
if (i==5){
Thread.yield();
}
}
};
Thread thread = new Thread(runnable,"thread1");
Thread thread2 = new Thread(runnable, "thread2");
thread.setPriority(1);
thread2.setPriority(9);
thread.start();
thread2.start();
}
}
临界资源问题
假设某电影院有100张电影票,由4个窗口同时进行销售。
public class Ticket {
public static void main(String[] args) {
Runnable runnable = () -> {
while (TicketCount.count > 0) {
//同步代码片段,对象锁(字符串),多个线程看到的锁需要是同一把锁
synchronized ("") {
if (TicketCount.count < 0) {
return;
}
String name = Thread.currentThread().getName();
System.out.println(name + "卖掉了一张,剩余" + TicketCount.count--);
}
}
};
Thread seller1 = new Thread(runnable, "seller1");
Thread seller2 = new Thread(runnable, "seller2");
Thread seller3 = new Thread(runnable, "seller3");
Thread seller4 = new Thread(runnable, "seller4");
seller1.start();
seller2.start();
seller3.start();
seller4.start();
}
}
class TicketCount {
static int count = 100;
}
修改:
package org.youyuan.thread;
public class Ticket {
public static void main(String[] args) {
Runnable runnable = () -> {
while (TicketCount.count > 0) {
sellTickers();
}
};
Thread seller1 = new Thread(runnable, "seller1");
Thread seller2 = new Thread(runnable, "seller2");
Thread seller3 = new Thread(runnable, "seller3");
Thread seller4 = new Thread(runnable, "seller4");
seller1.start();
seller2.start();
seller3.start();
seller4.start();
}
/*同步的方法
*静态方法:同步锁就是类锁 当前类 .classs
* 非静态方法:同步锁是 this*/
private static synchronized void sellTickers() {
if (TicketCount.count <= 0) {
return;
}
String name = Thread.currentThread().getName();
System.out.println(name + "卖掉了一张,剩余" + --TicketCount.count);
}
}
class TicketCount {
static int count = 100;
}
还可以实例化一个锁对象
public class Lock {
public static void main(String[] args) {
/*
* 实例化一个锁对象
* */
ReentrantLock lock = new ReentrantLock();
Runnable runnable = () -> {
while (TicketCount.count > 0) {
//对临界资源上锁
lock.lock();
if (TicketCount.count < 0) {
return;
}
String name = Thread.currentThread().getName();
System.out.println(name + "卖掉了一张,剩余" + TicketCount.count--);
//解锁
lock.unlock();
}
};
Thread seller1 = new Thread(runnable, "seller1");
Thread seller2 = new Thread(runnable, "seller2");
Thread seller3 = new Thread(runnable, "seller3");
Thread seller4 = new Thread(runnable, "seller4");
seller1.start();
seller2.start();
seller3.start();
seller4.start();
}
}