package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (this){ //使用this对象锁
//synchronized (MyThread.class){ //使用MyThread.class类锁
//synchronized (""){//使用字符串锁
System.out.println(Thread.currentThread().getName() + "正在运行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "线程结束");
}
}
}
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
MyThread myThread = new MyThread();
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(myThread,"线程"+i); //开启5个线程,传入同一个对象
thread.start();
}
}
}
线程1正在运行
9
线程1线程结束
线程3正在运行
8
线程3线程结束
线程5正在运行
7
线程5线程结束
线程2正在运行
6
线程2线程结束
线程4正在运行
5
线程4线程结束
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (this){//使用this对象锁
System.out.println(Thread.currentThread().getName() + "正在运行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "线程结束");
}
}
}
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"线程"+i);//开启5个线程,传入5个不同对象
thread.start();
}
}
}
线程2正在运行
线程1正在运行
线程3正在运行
线程5正在运行
线程4正在运行
9
7
9
8
线程1线程结束
线程5线程结束
线程2线程结束
线程3线程结束
6
线程4线程结束
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (MyThread.class){//使用MyThread.class类锁
System.out.println(Thread.currentThread().getName() + "正在运行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "线程结束");
}
}
}
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"线程"+i); //开启5个线程,传入5个不同对象
thread.start();
}
}
}
线程1正在运行
9
线程1线程结束
线程5正在运行
8
线程5线程结束
线程4正在运行
7
线程4线程结束
线程3正在运行
6
线程3线程结束
线程2正在运行
5
线程2线程结束
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (""){//使用字符串锁
System.out.println(Thread.currentThread().getName() + "正在运行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "线程结束");
}
}
}
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"线程"+i); //开启5个线程,传入5个不同对象
thread.start();
}
}
}
线程1正在运行
9
线程1线程结束
线程4正在运行
8
线程4线程结束
线程5正在运行
7
线程5线程结束
线程3正在运行
6
线程3线程结束
线程2正在运行
5
线程2线程结束
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有