多线程实现流程
- 定义一个MyThread类继承Thread类
- 在MyThread类中重写run()方法
- 创建MyThread类的对象
- 启动线程
setName()和getName()
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
MyThread s = new MyThread();
s.setName("倒计时:"); //设置线程名称为倒计时:
s.start();
while (true){
String time = String.valueOf(s.getTime());
if ("0".equals(time)) {
Thread.sleep(1);
System.out.println("时间到");
break;
}
}
}
}
public class MyThread extends Thread{
private int time=-1;
public int getTime() {
return this.time;
}
@Override
public void run() {
for (int i = 5; i >=0 ; i--) {
this.time = i;
System.out.println(getName()+this.time); //获取线程名称
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程优先级设置
- setPriority()
- getPriority()
```java
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
} }MyThread s = new MyThread();
s.start();
s.setPriority(2); //设置线程优先级为2 范围为1-10
while (true){
String time = String.valueOf(s.getTime());
if ("0".equals(time)) {
Thread.sleep(1);
System.out.println("时间到");
break;
}
}
public class MyThread extends Thread{ private int time=-1; public int getTime() { return this.time; } @Override public void run() { for (int i = 5; i >=0 ; i—) { this.time = i; System.out.println(“该线程优先级:”+getPriority()+”\n倒计时”+this.time); //获取线程优先级 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
<a name="h2sTU"></a>
## 线程控制
- Thread.sleep()
- *.join()
- *.setDaemon()
```java
public class MyThread extends Thread{
private int time;
private int i;
public int getTime() {
return this.time;
}
public void seti(int i) {
this.i = i;
}
@Override
public void run() {
for (int i = this.i; i >=0 ; i--) {
this.time = i;
System.out.println("倒计时"+this.time);
try {
Thread.sleep(1000); //设置该线程等待1s后运行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
MyThread s1 = new MyThread();
MyThread s2 = new MyThread();
MyThread s3 = new MyThread();
s1.seti(5);
s2.seti(10);
s3.seti(5);
s2.setDaemon(true); //设置s2为守护线程,当只剩下守护线程在运行时,jvm虚拟机会退出
s1.start();
s1.join(); //设置当s1执行完后其他线程才会执行
s2.start();
s3.start();
}
}
通过实现Runnable接口实现多线程
public class MyThread implements Runnable{
private int time;
public void run(){
this.time = 100;
while (true){
if (this.time <= 0){
break;
}
System.out.println(Thread.currentThread().getName()+"正在出售第"+time+"张票");
this.time--;
}
}
}
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
MyThread r = new MyThread();
Thread s1 = new Thread(r,"一号窗口");
Thread s2 = new Thread(r,"二号窗口");
Thread s3 = new Thread(r,"三号窗口");
s1.start();
s2.start();
s3.start();
}
}
//使用该方式开启多线程时使用Thread.currentThread().getName()访问该进程名
同步代码块
public class MyThread implements Runnable{
private int time = 100;
private Object obj = new Object();
public void run(){
while (true){
synchronized (obj){
if (this.time <= 0){
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"正在出售第"+time+"张票");
this.time--;
}
}
}
}
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
MyThread r = new MyThread();
Thread s1 = new Thread(r,"一号窗口");
Thread s2 = new Thread(r,"二号窗口");
Thread s3 = new Thread(r,"三号窗口");
s1.start();
s2.start();
s3.start();
}
}
同步方法
public class MyThread implements Runnable{
private int time = 100;
public void run(){
while (true){
f();
}
}
private synchronized void f(){
if (this.time <= 0){
System.out.println("出售完毕");
System.exit(0);
}
else {
System.out.println(Thread.currentThread().getName()+this.time);
this.time--;
}
}
}
public class MyThreadDemo {
public static void main(String[] args) throws InterruptedException {
MyThread r = new MyThread();
Thread s1 = new Thread(r,"一号窗口");
Thread s2 = new Thread(r,"二号窗口");
Thread s3 = new Thread(r,"三号窗口");
s1.start();
s2.start();
s3.start();
}
}