1.创建线程的4种方式
方式一:
继承Thread,缺点,java是单继承,如果继承了Thread就不能继承其他类
方式二:
实现runnable()接口,通常不直接在类上实现runnable接口,与类的耦合度高
方式三:实现callable()接口 和futrue使用
与实现runnable接口相比,callable()有返回值,且自带异常处理
方式四:使用线程池
2.各种锁的java实现方式
通俗易懂 悲观锁、乐观锁、可重入锁、自旋锁、偏向锁、轻量/重量级锁、读写锁、各种锁及其Java实现!
-------------------------------
ThreadDemo类
package com.javasm;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
public class UserInfo {
final Object object = new Object();
private Integer age;
private String name;
private Integer balance;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public UserInfo(Integer age, String name, Integer balance) {
this.age = age;
this.name = name;
this.balance = balance;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
protected boolean flag = true;
int count = 0;
public void getMoney() {
while (flag) {
synchronized (this) {
if (balance <= 0) {
System.out.println("余额不足");
flag = false;
return;
}
count++;
balance -= 1000;
System.out.println(Thread.currentThread().getName() + "取走了"+name+"1000元" + "剩余:" + balance + "-" + "第" + count + "次取钱");
}
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void setMoney() {
balance += 1000;
System.out.println(Thread.currentThread().getName() + "往账户存入1000" + "剩余:" + balance+"元");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
--------------------------
UserInfo类
package com.javasm;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
public class UserInfo {
final Object object = new Object();
private Integer age;
private String name;
private Integer balance;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public UserInfo(Integer age, String name, Integer balance) {
this.age = age;
this.name = name;
this.balance = balance;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
protected boolean flag = true;
int count = 0;
public void getMoney() {
while (flag) {
synchronized (this) {
if (balance <= 0) {
System.out.println("余额不足");
flag = false;
return;
}
count++;
balance -= 1000;
System.out.println(Thread.currentThread().getName() + "取走了"+name+"1000元" + "剩余:" + balance + "-" + "第" + count + "次取钱");
}
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public void setMoney() {
balance += 1000;
System.out.println(Thread.currentThread().getName() + "往账户存入1000" + "剩余:" + balance+"元");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
2.如何解决线程安全问题?
标签:JAVA,name,age,---,flag,线程,Integer,balance,public From: https://www.cnblogs.com/wang1999an/p/16875293.html