- 并行:即同一时刻多条指令,在多个cpu上.同时进行
- 并发:即同一时刻多条指令,在单个cpu交替执行
- 进程:即正在运行的软件,具有独立性、动态性、并发性
- 线程:进程中单个顺序控制流,简单来说是一条执行路径,即应用程序想做的事
- 单线程:即一个进程只有一条执行路径
- 多线程:即一个进程有多条执行路径
- 实现线程的三种方式
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName()+"第"+i+"次表白");
}
}
}
ublic class Test01 {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyThread myThread1 = new MyThread();
myThread.setName("蔡根花");
myThread1.setName("大强");
myThread.start();
myThread1.start();
ublic class Test02 {
public static void main(String[] args) {
MyRunnble myRunnble = new MyRunnble();
Thread thread = new Thread(myRunnble);
Thread thread1 = new Thread(myRunnble);
thread.setName("你");
thread1.setName("我");
thread.start();
thread1.start();
}
}
public class MyRunnble implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"多线程接口实现方式二"+i);
}
}
}
public class Test03 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable myCallable = new MyCallable();
FutureTask<Boolean> futureTask = new FutureTask(myCallable);
Thread thread = new Thread(futureTask);
thread.setName("李白");
thread.setPriority(9);
thread.start();
MyCallable myCallable1 = new MyCallable();
FutureTask<Boolean> futureTask1 = new FutureTask(myCallable1);
Thread thread1 = new Thread(futureTask1);
thread1.setName("王维");
thread1.setPriority(6);
thread1.start();
}
}
public class MyCallable implements Callable<Boolean> {
@Override
public Boolean call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 99; i++) {
System.out.println(Thread.currentThread().getName()+"跟女孩表白"+i+"次");
}
return true;
}
}
- 线程常用方法:getname()获取线程名称 setname()设置线程名称 ,thread继承类能直接使用,但Runnble和callable接口实现类不能直接使用,需通过Thread.currentThread()先获取线程对象才能调用
- sleep()线程休眠 thread类名直接调用
- 线程调度:分时调度模型:即所有线程轮流使用cpu使用权,即平均分配 抢占式调度模型: 即相对优先级高的线程抢占的几率较大
- getPriority() 获取当前线程的优先级 setPriority() 设置线程优先级(1-10之间)
- 守护线程:为守护普通线程而存在 setDaemon()设置守护线程
- 同步代码块:默认情况下是打开的,当有线程进去自动锁上
public class ticket implements Runnable {
private int sum = 100;
private final Object object = new Object();
@Override
public void run() {
while (true){
synchronized (object){
if (sum<=0){
break;
}else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
sum--;
System.out.println(Thread.currentThread().getName()+"正在卖票还剩下"+sum+"票");
}
}
}
}
}
public class Test08 {
public static void main(String[] args) {
ticket ticket = new ticket();
Thread thread1 = new Thread(ticket);
Thread thread2 = new Thread(ticket);
thread1.setName("窗口一");
thread2.setName("窗口二");
thread1.start();
thread2.start();
}
}
- 同步方法:即在方法返回值前面加synchronized
public class MyRunnble1 implements Runnable {
private static int sum = 100;
@Override
public void run() {
while (true){
if ("窗口一".equals(Thread.currentThread().getName())){
boolean synchronizedmethods = Synchronizedmethods();
if (synchronizedmethods){
break;
}
}
if ("窗口二".equals(Thread.currentThread().getName())){
synchronized (MyRunnble1.class){
if (sum==0){
break;
}else {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
sum--;
System.out.println(Thread.currentThread().getName()+"正在卖票还剩下"+sum+"张票");
}
}
}
}
}
private static synchronized boolean Synchronizedmethods() {
if (sum==0){
return true;
}else {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
sum--;
System.out.println(Thread.currentThread().getName()+"正在卖票还剩下"+sum+"张票");
return false;
}
}
}
public class Test07 {
public static void main(String[] args) {
MyRunnble1 myRunnble1 = new MyRunnble1();
Thread thread1 = new Thread(myRunnble1);
Thread thread2 = new Thread(myRunnble1);
thread1.setName("窗口一");
thread2.setName("窗口二");
thread1.start();
thread2.start();
}
}
- lock锁:直接的显示怎么上锁、解锁
- 创建lock接口实现类对象 ReentrantLock lock = new ReentrantLock(); unlock()释放锁 lock()获得锁
- 死锁:即锁的嵌套造成
- 生产者、消费者模式:
public class producers extends Thread {
@Override
public void run() {
while (true){
synchronized (table.object){
if (table.sum==0){
break;
}else {
if (!table.foot){
System.out.println(getName()+"正在生产食物");
table.foot = true;
table.object.notifyAll();
}else {
try {
table.object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
public class consumers extends Thread{
@Override
public void run() {
while (true){
synchronized (table.object){
if (table.sum==0){
break;
}else {
if (table.foot){
System.out.println(getName()+"正在吃");
table.foot =false;
table.object.notifyAll();
table.sum--;
}else {
try {
table.object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
public class Test02 {
public static void main(String[] args) {
producers producers = new producers();
consumers consumers = new consumers();
producers.setName("生产者");
consumers.setName("消费者");
producers.start();
consumers.start();
}
}
public class producer1 extends Thread {
private final ArrayBlockingQueue<String> arr;
public producer1(ArrayBlockingQueue<String> arr) {
this.arr= arr;
}
@Override
public void run() {
while (true) {
try {
arr.put("汉堡");
System.out.println("厨师放了一个汉堡包");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class consumer1 extends Thread {
private final ArrayBlockingQueue<String> arr;
public consumer1(ArrayBlockingQueue<String> arr) {
this.arr= arr;
}
@Override
public void run() {
while (true) {
try {
String take = arr.take();
System.out.println("吃货正在吃"+take);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test04 {
public static void main(String[] args) {
ArrayBlockingQueue<String> arr = new ArrayBlockingQueue<>(1);
consumer1 consumer1 = new consumer1(arr);
producer1 producer1 = new producer1(arr);
consumer1.start();
producer1.start();
}
}