简单讲解了关于多线程的一些应用:线程进程的概念区别,定义多线程,多线程之间的通信,线程的死锁,以及线程的优先级。此类问题在Java面试当中经常触及。
多线程.png
1.多线程的实践:
package Thread;
public class Live implements Runnable{
public void run() {
for(int i = 1;i<=6;i++){
System.out.println("抠脚"+i+"次");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package Thread;
public class Play extends Thread{
public void run(){
for(int i=1;i<=30;i++){
System.out.println("who打单机:第"+i+"局,输了,玩的真菜!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package Thread;
/*
* 多线程同时执行,有两种实现多线程的方法,一种是继承类,一种是实现接口。
* 继承: 继承Thread的话,该子类应重写 Thread 类的 run 方法。接下来可以分配并启动该子类的实例
* 实现接口:声明实现 Runnable 接口的类,该类然后实现 run 方法。
* 然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。
* */
public class Thread_jichu {
public static void print(){
for(int i=1;i<=50;i++){
System.out.println("who看校花的贴身高手:第"+i+"集,看的挺嗨的!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t = new Play();
Live l = new Live();
Thread t1 = new Thread(l);
t1.start();
t.start();
print();
}
}
2.线程安全和同步:
package Thread;
public class Synchronized_tongbu {
public static void main(String[] args) {
BankAccount account = new BankAccount();
Thread husband = new BankThread(account);
Thread wife = new BankThread(account);
husband.start();
wife.start();
}
}
class BankAccount{
private double balance = 1000;
public boolean depasit(double newAdd){
if(newAdd <=0){
return false;
}else{
synchronized(this){//锁方法和锁局域代码
System.out.println("当前余额为:"+balance);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
balance = balance + newAdd;
}
System.out.println("新的余额为:" + balance);
}
return true;
}
}
class BankThread extends Thread{
private BankAccount bankAccount;
public BankThread(BankAccount account){
bankAccount = account;
}
public void run(){
bankAccount.depasit(200);
}
}
死锁
package Thread;
/*
* 由于加锁,所以在取资源时某一方没有使用完不得进入该空间。
*
* */
public class DeadLock {
public static void main(String[] args) {
Object object1 = new Object();
Object object2 = new Object();
Thread ta = new A(object1, object2);
Thread tb = new B(object1, object2);
ta.start();
tb.start();
}
}
class A extends Thread {
Object object1;
Object object2;
public A(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
public void run() {
synchronized (object1) {
System.out.println("A取到了object1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(object2){
System.out.println("A拿到了object1和object2");
}
}
}
}
class B extends Thread {
public Object object1;
public Object object2;
public B(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
public void run() {
synchronized (object2) {
System.out.println("B取到了object1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(object1){
System.out.println("B拿到了object1和object2");
}
}
}
}
如上代码会出现死锁,未能A B都未能取到所需,将B run方法中object2 object1调换即可。取得顺序相同很庆幸解决了问题。
网友评论