创建线程
继承Thread类创建线程
public class MyThread extends Thread{
@Override
public void run() {
for (int i
= 0; i
< 100; i
++) {
System
.out
.println("run:"+i
);
}
}
}
public class MainThread {
public static void main(String
[] args
) {
MyThread task
= new MyThread();
task
.start();
for (int i
= 0; i
< 100; i
++) {
System
.out
.println("main:"+i
);
}
}
}
实现Runable接口创建线程
public class RunableImp implements Runnable {
@Override
public void run() {
for (int i
= 0; i
< 100; i
++) {
System
.out
.println("run:" + i
);
}
}
}
public class MainThread {
public static void main(String
[] args
) {
RunableImp r
= new RunableImp();
Thread t
= new Thread(r
);
t
.start();
for (int i
= 0; i
< 100; i
++) {
System
.out
.println("main:" + i
);
}
}
}
匿名内部类创建线程(Thread类)
public class MainThread {
public static void main(String
[] args
) {
new Thread(){
@Override
public void run() {
for (int i
= 0; i
< 100; i
++) {
System
.out
.println(i
);
}
}
}.start();
}
}
匿名内部类创建线程(Runable接口)
public class MainThread {
public static void main(String
[] args
) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i
= 0; i
< 100; i
++) {
System
.out
.println(i
);
}
}
}).start();
}
}
线程安全
线程安全问题代码实现
public class RunableImp implements Runnable {
private int ticket
= 100;
@Override
public void run() {
while (true){
if(ticket
> 0){
try {
Thread
.sleep(10);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println(Thread
.currentThread().getName()+":正在卖第"+ticket
+"票");
ticket
--;
}
}
}
}
public class Ticket {
public static void main(String
[] args
) {
RunableImp r
= new RunableImp();
Thread t0
= new Thread(r
);
Thread t1
= new Thread(r
);
Thread t2
= new Thread(r
);
t0
.start();
t1
.start();
t2
.start();
}
}
线程同步
1.同步代码块
2.同步方法
3.锁机制
同步代码块
public class RunableImp implements Runnable {
private int ticket
= 100;
Object obj
= new Object();
@Override
public void run() {
while (true){
synchronized (obj
){
if(ticket
> 0){
try {
Thread
.sleep(10);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println(Thread
.currentThread().getName()+":正在卖第"+ticket
+"票");
ticket
--;
}
}
}
}
}
同步方法
非静态方法实现同步
public class RunableImp implements Runnable {
private int ticket
= 100;
@Override
public void run() {
System
.out
.println("this:"+this);
while (true) {
PayTicket();
}
}
public synchronized void PayTicket(){
if (ticket
> 0) {
try {
Thread
.sleep(10);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println(Thread
.currentThread().getName() + ":正在卖第" + ticket
+ "票");
ticket
--;
}
}
}
public class Ticket {
public static void main(String
[] args
) {
RunableImp r
= new RunableImp();
System
.out
.println("r:"+r
);
Thread t0
= new Thread(r
);
Thread t1
= new Thread(r
);
Thread t2
= new Thread(r
);
t0
.start();
t1
.start();
t2
.start();
}
}
静态方法实现同步
public static void PayTicket(){
synchronized(RunableImp
.class) {
if (ticket
> 0) {
try {
Thread
.sleep(10);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
System
.out
.println(Thread
.currentThread().getName() + ":正在卖第" + ticket
+ "票");
ticket
--;
}
}
}
同步锁:对于非static方法,同步锁就是this。对于static方法,同步锁是当前方法所在类的字节码对象(类名.class)。
Lock锁
import java
.util
.concurrent
.locks
.Lock
;
import java
.util
.concurrent
.locks
.ReentrantLock
;
public class RunableImp implements Runnable {
private static int ticket
= 100;
Lock l
= new ReentrantLock();
@Override
public void run() {
System
.out
.println("this:" + this);
while (true) {
l
.lock();
if (ticket
> 0) {
try {
Thread
.sleep(10);
System
.out
.println(Thread
.currentThread().getName() + ":正在卖第" + ticket
+ "票");
ticket
--;
} catch (InterruptedException e
) {
e
.printStackTrace();
} finally {
l
.unlock();
}
} else {
l
.unlock();
break;
};
}
}
}
转载请注明原文地址:https://blackberry.8miu.com/read-18206.html