java多线程:
创建多线程有四种方法
程序:一段静态的代码
进程:正在运行的程序
线程:一个程序内部的一条执行路径
为什么不用 .run();
常用方法:
优先级不是绝对的。
三个窗口卖票
方法一:
public class windowstest {
public static void main(String[] args) {
window t1=new window();
window t2=new window();
window t3=new window();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
class window extends Thread{
private static int ticket=100;//用static静态才能正常
@Override
public void run() {
while(true){
if(ticket>0){
System.out.println(getName()+": 买票,票号为:"+ticket);
ticket--;
}else{
break;
}
}
}
}
方法二:
public class windowtest2 {
public static void main(String[] args) {
mthread m = new mthread();
Thread t2 = new Thread(m);
Thread t3 = new Thread(m);
Thread t4 = new Thread(m);
t2.setName("窗口一:");
t3.setName("窗口二:");
t4.setName("窗口三:");
t2.start();
t3.start();
t4.start();
}
}
class mthread implements Runnable{
private int ticket=100;//用Runnable不用加static
@Override
public void run() {
while(true){
if(ticket>0){
System.out.println(Thread.currentThread().getName()+": 买票,票号为:"+ticket);
ticket--;
}else{
break;
}
}
}
}
解决线程安全问题:
方法一:同步代码块
synchronized(同步监视器){ }
不能包多,也不能包少了
接口实现多线程
class mthread implements Runnable{
private int ticket=100;//用Runnable不用加static
public void run() {
while(true) {
synchronized (this) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": 买票,票号为:" + ticket);
ticket--;
} else {
break;
}
}
}
}
}
Runnable接口用this很方便
IDEA格式化快捷键:
ctrl+alt+L
子类继承多线程
public class windowstest {
public static void main(String[] args) {
window t1 = new window();
window t2 = new window();
window t3 = new window();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
class window extends Thread {
private static int ticket = 100;//用static静态才能正常
public void run() {
while (true) {
synchronized (window.class) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(getName() + ": 买票,票号为:" + ticket);
ticket--;
} else {
break;
}
}
}
}}
window.class只会加载一次
方法二:
public synchronized void show() { }(同步方法)
Runnable接口多线程
public class windowtest2 {
public static void main(String[] args) {
mthread m = new mthread();
Thread t2 = new Thread(m);
Thread t3 = new Thread(m);
Thread t4 = new Thread(m);
t2.setName("窗口一:");
t3.setName("窗口二:");
t4.setName("窗口三:");
t2.start();
t3.start();
t4.start();
}
}
class mthread implements Runnable {
private int ticket = 100;//用Runnable不用加static
public void run() {
while (true) {
show();
}
}
public synchronized void show() { //同步监视器是this
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": 买票,票号为:" + ticket);
ticket--;
}
}
}
子类继承多线程
public class windowstest {
public static void main(String[] args) {
window t1 = new window();
window t2 = new window();
window t3 = new window();
t1.setName("窗口1");
t2.setName("窗口2");
t3.setName("窗口3");
t1.start();
t2.start();
t3.start();
}
}
class window extends Thread {
private static int ticket = 100;//用static静态才能正常
public void run() {
while (true) {
show();
}
}
public static synchronized void show(){ //同步监视器:window.class
//public synchronized void show() { 同步监视器:t1,t2,t3必须加static才行
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": 买票,票号为:" + ticket);
ticket--;
}
}
}
synchronized前面要加static
死锁
包围快捷键:
方法三:lock