一、实现Runable接口,重新run方法
1、同步代码块
class Window implements Runnable{
private int ticket = 100;
Object obj = new Object();
@Override
public void run() {
while (true) {
synchronized (obj) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":票号为:" + ticket);
ticket--;
} else {
break;
}
}
}
}
}
public class ThreadTest1 {
public static void main(String[] args) {
Window test1 = new Window();
Thread thread1 = new Thread(test1);
Thread thread2 = new Thread(test1);
Thread thread3 = new Thread(test1);
thread1.setName("窗口一");
thread2.setName("窗口二");
thread3.setName("窗口三");
thread1.start();
thread2.start();
thread3.start();
}
}
2、同步方法
private int ticket = 100;
@Override
public void run() {
while (true){
show();
if (ticket <= 0){
break;
}
}
}
public synchronized void show(){
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":票号为:" + ticket);
ticket--;
}
}
3、加锁还可以是lock锁
@Override
public void run() {
while (true){
try {
lock.lock();
if (ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" +ticket);
ticket--;
}else {
break;
}
} finally {
lock.unlock();
}
}
}
二、继承Thread类重写run方法
public static void main(String[] args) {
ThreadTest1 thread1 = new ThreadTest1();
Thread thread = new Thread(thread1);
thread.start();
Thread thread2 = new Thread(thread1);
thread2.start();
}
class ThreadTest1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
}
三、实现Callable接口,重新run方法
public class CallableTest {
//可以抛出异常,支持泛型
public static void main(String[] args) {
//2、创建实现了Callable接口的对象
NumThread thread = new NumThread();
//3、将实现了Callable接口的对象作为类的对象传递给FutureTask的构造器中
FutureTask futureTask = new FutureTask(thread);
//4、FutureTask的对象作为类的对象传递给Thread构造器中
new Thread(futureTask).start();
try {
Object sum = futureTask.get();
//get的返回值就是FutureTask 构造参数Callable的call方法
System.out.println(sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//1、创建一个类实现Callable接口,实现call方法
class NumThread implements Callable{
@Override
public Object call() throws Exception {
int sum = 0;
for (int i = 0; i < 50; i++) {
if (i % 2 == 0){
System.out.println(i);
sum += i;
}
}
return sum;
}
}
四、使用线程池创建线程
public class ThreadPollTest {
private ExecutorService service;
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(10);
ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
System.out.println(service.getClass());
service.execute(new NumberThread());
service.shutdown();
}
}
class NumberThread implements Runnable {
@Override
public void run() {
for (int i = 0; i < 50; i++) {
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName() + i);
}
}
}
}
五、多线程常用方法
Thread.yield();
//让出CPU执行权,自己和其他线程一起再次抢占
start();
//线程开启
sleep(long millis)
//开启睡眠状态,过了millis毫秒时开始执行,不释放锁
setPriority(int newPriority)
getPriority()
//设置和得到线程的优先级,1-10,默认是5
setName(String name)
getName()
//设置线程的名字和得到名字
setDaemon(boolean on)
//设置线程为守护线程
join()
//加入新的一个线程,并且需要新的线程执行完,这个线程才可以执行
interrupt()
//中断线程
interrupted()
//判断线程中断
getState()
//得到线程的状态