- 进程是一个具有一定独立功能的程序关于某个数据集合的一次运行活动
- 线程是在进程中独立运行的子任务
- 线程作用:提高程序运行效率,注意并不能提高程序运行速度,且同一个CPU同一时间只能运行一个线程
4.实例:公司有三条生产线,仓库有3000kg原料,一条生产线生产一个机器要100kg原料,用程序模拟
package day15;
public class Demo {
public static void main(String[] args) {
Production p = new Production();
new Thread(new Production()).start();
new Thread(new Production()).start();
new Thread(new Production()).start();
}
static class Production implements Runnable {
//3000kg原料
static int number = 3000;
//生产机器数量
static int numbers = 0;
@Override
public void run() {
while (number > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.print(Thread.currentThread().getName() + "原料:" + number);
System.out.println("机器数量:" + numbers);
}
}
}
}
因为在判断number>0时,程序休眠了0.1秒,导致多个线程都进入了循环过程,于是出现了错误
所以需要加入锁
package day15;
public class Demo {
public static void main(String[] args) {
Production p = new Production();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
}
static class Production implements Runnable {
//3000kg原料
int number = 3000;
//生产机器数量
int numbers = 0;
@Override
public void run() {
while (produce()) {
}
}
public synchronized boolean produce() {
if (number > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.println(Thread.currentThread().getName() + "原料:" + number + "机器数量:" + numbers);
return true;
}
return false;
}
}
}
package day15;
public class Demo {
public static void main(String[] args) {
Production p = new Production();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
}
static class Production implements Runnable {
//3000kg原料
int number = 3000;
//生产机器数量
int numbers = 0;
@Override
public synchronized void run() {
while (number > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.println(Thread.currentThread().getName() + "原料:" + number + "机器数量:" + numbers);
}
}
public synchronized boolean produce() {
if (number > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.println(Thread.currentThread().getName() + "原料:" + number + "机器数量:" + numbers);
return true;
}
return false;
}
}
}
synchronized修饰的方法会进行排序,其有对应的对象锁,为其本身对象
package day15;
public class Demo {
public static void main(String[] args) {
Production p = new Production();
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
}
static class Production implements Runnable {
//3000kg原料
int number = 3000;
//生产机器数量
int numbers = 0;
static Object o = new Object();
@Override
public void run() {
synchronized (o) {
while (number > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.println(Thread.currentThread().getName() + "原料:" + number + "机器数量:" + numbers);
}
}
}
public synchronized boolean produce() {
if (number > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
number = number - 100;
numbers++;
System.out.println(Thread.currentThread().getName() + "原料:" + number + "机器数量:" + numbers);
return true;
}
return false;
}
}
}
对象锁需为同一对象
以上都是锁的简单应用