举例:
public class Data {
public static void main(String[] args) {
Test test=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test.run();
},"B").start();
}
}
class Test {
public void eat(){
try{
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public void run(){
System.out.println("============2=run");
}
}
结果:
============2=run
============1=eat
Process finished with exit code 0
再次修改:
public class Data {
public static void main(String[] args) {
Test test=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test.run();
},"B").start();
}
}
class Test {
public synchronized void eat(){
try{
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public synchronized void run(){
System.out.println("============2=run");
}
}
输出结果:非静态类:锁定的是方法的调用者
============1=eat
============2=run
Process finished with exit code 0
再次修改:去掉run方法的synchronized 关键字
public class Data {
public static void main(String[] args) {
Test test=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test.run();
},"B").start();
}
}
class Test {
public synchronized void eat(){
try{
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public void run(){
System.out.println("============2=run");
}
}
此时不需要争夺资源,A线程走完后 ,直接走run方法
结果:
============2=run
============1=eat
Process finished with exit code 0
再次修改,创建2个对象,不同对象调不同方法
public class Data {
public static void main(String[] args) {
Test test=new Test();
Test test1=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test1.run();
},"B").start();
}
}
class Test {
public synchronized void eat(){
try{
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public synchronized void run(){
System.out.println("============2=run");
}
}
结果:
============2=run
============1=eat
Process finished with exit code 0
修饰静态方法示例:
public class Data {
public static void main(String[] args) {
Test test=new Test();
Test test1=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);//睡眠1秒
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test1.run();
},"B").start();
}
}
class Test {
public synchronized static void eat(){
try{
TimeUnit.SECONDS.sleep(3);//睡眠3秒
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public synchronized static void run(){
System.out.println("============2=run");
}
}
结果:
============1=eat
============2=run
Process finished with exit code 0
此时需要等待 ,锁定的是Test类
再次修改:
public class Data {
public static void main(String[] args) {
Test test=new Test();
Test test1=new Test();
//线程A
new Thread(()->{
test.eat();
},"A").start();
try{
TimeUnit.SECONDS.sleep(1);//睡眠1秒
}catch (InterruptedException e){
e.printStackTrace();
}
//线程B
new Thread(()->{
test1.run();
},"B").start();
}
}
class Test {
public synchronized static void eat(){
try{
TimeUnit.SECONDS.sleep(3);//睡眠3秒
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("============1=eat");
}
public static void run(){
System.out.println("============2=run");
}
}
结果:b线程调取方法没有 synchronized 不用锁定 直接执行就可以了
============2=run
============1=eat
Process finished with exit code 0
-
修饰方法:
(1).静态方法 :锁定的是类
(2).非静态方法:锁定的是方法的调用者
-
修饰代码块
锁定是传入的对象