首页 技术 正文
技术 2022年11月8日
0 收藏 357 点赞 1,791 浏览 2525 个字

实现多线程的两种方式

继承Thread类,重写Thread类中的run方法

public class MyThread extends Thread{
@Override
public void run(){
super.run();
System.out.println("this is myThread run");
}
}
public static void main(String[] args) {
Thread myThread=new MyThread();
myThread.start();
System.out.println("mian function is over");
}

注意:代码的顺序并不是线程的执行顺序,start的顺序也不是多个线程的执行顺序。

实现Runable接口

java是单继承的,一个子类只能去继承一个父类,所以如果线程类已经继承了其他父类,那么就不能采用继承thread类来实现多线程了

public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("this is myrunable running");
}
}
public class Test {
public static void main(String[] args) {
Runnable myRunnable=new MyRunnable();
Thread thread=new Thread(myRunnable);
thread.start();
System.out.println("main is runing");
}
}

 实例变量与线程安全

数据不共享的实例,两个线程中分别创建了两个thread实例,各种都是独立的count

public class MyThread extends Thread{
private int count=5;
public MyThread(String name) {
super();
this.setName(name);
}
@Override
public void run(){
super.run();
while(count>0){
count--;
System.out.println(this.currentThread().getName());
System.out.println(count);
}
}
}
public class Test {
public static void main(String[] args) {
Thread thread1=new MyThread("A");
Thread thread2=new MyThread("B");
thread1.start();
thread2.start();
}
}

数据共享的实例

public class MyThread extends Thread {
private int count = 5;
@Override
public void run() {
super.run();
count--;
System.out.println(this.currentThread().getName()+count);
}
}
public class Test {
public static void main(String[] args) {
Thread thread=new MyThread(); Thread thread1=new Thread(thread, "A");
Thread thread2=new Thread(thread,"B");
Thread thread3=new Thread(thread, "C");
thread1.start();
thread2.start();
thread3.start();
}
}

可以在方法上加同步锁,syschronized,这样每一个线程在执行这个方法前需要先尝试去获取这把锁,获取不到就会等待,一直到获取到。

public class MyThread extends Thread {
private int count = 5;
@Override
synchronized public void run() {
super.run();
count--;
System.out.println(this.currentThread().getName()+count);
}
}

下面是一个多线程非安全对的共享变量实例:

在实例中共享了静态变量usanameRef和passwordRef

public class Alogin extends Thread{
@Override
public void run(){
LoginServlet.doPost("a", "aa");
}
}
public class Blogin extends Thread{
@Override
public void run(){
LoginServlet.doPost("b", "bb");
}
}
public class LoginServlet {
private static String usenameRef;
private static String passwordRef;
synchronized public static void doPost(String usename,String password){
try {
usenameRef=usename; if(usename.equals("a")){
Thread.sleep(2000);
}
passwordRef=password;
System.out.println("username="+usenameRef+"password="+password);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
Alogin alogin=new Alogin();
alogin.start();
Blogin blogin=new Blogin();
blogin.start();
}
}

参考: 《java多线程编程核心技术》

https://blog.csdn.net/iaiti/article/details/53314149

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290