java 多线程跑数据_java——多线程的实现方式、三种办法解决线程赛跑、多线程数据同步(synchronized)、死锁...
多線程的實現方式:demo1、demo2
demo1:繼承Thread類,重寫run()方法
packagethread_test;public class ThreadDemo1 extendsThread {
ThreadDemo1(){
}
ThreadDemo1(String szName){super(szName);
}//重載run函數
public voidrun() {for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {for(int i = 0 ; i < count ; i ++) {
System.out.print("*");
}
System.out.println();
}
}public static voidmain(String[] args) {//線程賽跑
ThreadDemo1 td1 = newThreadDemo1();
ThreadDemo1 td2= newThreadDemo1();
ThreadDemo1 td3= newThreadDemo1();
td1.start();
td2.start();
td3.start();
}
}
demo2:實現runnable接口,實現run()方法
packagethread_test;public class ThreadDemo2 implementsRunnable{public voidrun() {for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {for(int i = 0 ; i < count ; i ++) {
System.out.print("*");
}
System.out.println();
}
}public static voidmain(String[] args) {//存在線程賽跑問題
Runnable rb1 = newThreadDemo2();
Runnable rb2= newThreadDemo2();
Runnable rb3= newThreadDemo2();
Thread td1= newThread(rb1);
Thread td2= newThread(rb2);
Thread td3= newThread(rb3);
td1.start();
td2.start();
td3.start();
}
}
demo3:兩種方法解決進程賽跑問題
packagethread_test;//兩種方法解決線程賽跑
class ThreadWait extendsThread{publicThreadWait() {
}publicThreadWait(String name) {super(name);
}
@Overridepublic voidrun() {for(int count = 1 , row = 1 ; row < 10 ; row ++ , count ++) {for(int i = 0 ; i < count ; i ++) {
System.out.print("*");
}
System.out.println();
}
}
}public classThreadDemo3{public static voidmain(String[] args) {
ThreadDemo3 td= newThreadDemo3();//td.Method1();
td.Method2();
}public voidMethod1() {
ThreadWait tw1= newThreadWait();
ThreadWait tw2= newThreadWait();
tw1.start();while(tw1.isAlive()) {try{
Thread.sleep(100);
}catch(Exception e){
e.getMessage();
}
}
tw2.start();
}public voidMethod2() {
ThreadWait tw1= newThreadWait();
ThreadWait tw2= newThreadWait();
tw1.start();try{
tw1.join(); // 等待該線程中止
}catch(Exception e){
e.toString();
}
tw2.start();
}
}
線程異步訪問數據導致問題:
packagethread_test;//線程異步訪問數據導致問題
classShareData{public static String szData = "";
}class ThreadDemo extendsThread{private staticShareData oShare;
ThreadDemo(){
}
ThreadDemo(String name, ShareData oShare){super(name);this.oShare =oShare;
}public voidrun() {for(int i = 0 ; i < 5 ; i ++) {if(this.getName().equals("th1")) {
oShare.szData= "這是第一個進程";try{
Thread.sleep(100);
}catch(Exception e) {
}
System.out.println(this.getName() +oShare.szData);
}else if(this.getName().equals("th2")) {
oShare.szData= "這是第二個進程";try{
Thread.sleep(100);
}catch(Exception e) {
}
System.out.println(this.getName() +oShare.szData);
}
}
}
}public classThreadDemo5 {public static voidmain(String[] args) {
ShareData oShare= newShareData();
ThreadDemo th1= new ThreadDemo("th1", oShare);
ThreadDemo th2= new ThreadDemo("th2", oShare);
th1.start();
th2.start();
}
}
得到的結果并不是我們想要的:
解決辦法:
通過“鎖”解決線程賽跑問題并實現多線程數據同步:
packagethread_test;classShareData0{public static String szData = "";
}class ThreadDemo0 extendsThread{private staticShareData0 oShare;
ThreadDemo0(){
}
ThreadDemo0(String name, ShareData0 oShare){super(name);this.oShare =oShare;
}public voidrun() {//同步快,并指出同步數據oShare
synchronized(oShare){for(int i = 0 ; i < 5 ; i ++) {if(this.getName().equals("th1")) {
oShare.szData= "這是第一個進程";try{
Thread.sleep(100);
}catch(Exception e) {
}
System.out.println(this.getName() +oShare.szData);
}else if(this.getName().equals("th2")) {
oShare.szData= "這是第二個進程";try{
Thread.sleep(100);
}catch(Exception e) {
}
System.out.println(this.getName() +oShare.szData);
}
}
}
}
}public classThreadDemo6 {public static voidmain(String[] args) {
ShareData0 oShare= newShareData0();
ThreadDemo0 th1= new ThreadDemo0("th1", oShare);
ThreadDemo0 th2= new ThreadDemo0("th2", oShare);
th1.start();
th2.start();
}
}
得到結果:
死鎖:由于兩個線程都在等待對方釋放各自擁有的鎖的現象稱為死鎖,這種現象往往是由于相互潛逃的synchronized代碼段而造成的,所以少用synchronized嵌套。
下面是一個死鎖的例子:
packagethread_test;public class LockedThread extendsThread{private static Object A = newObject();private static Object B = newObject();private static boolean flag = true;public static voidmain(String[] args) {
LockedThread th1= newLockedThread();
LockedThread th2= newLockedThread();
th1.start();
th2.start();
}public voidAccessA() {
flag= false;synchronized(A) {
System.out.println("th1獲得了A的鎖");try{
Thread.sleep(1000);
}catch(Exception e) {
}
System.out.println("th1還想要B的鎖");synchronized(B) {
System.out.println("th1獲得了B的鎖");
}
}
}public voidAccessB() {
flag= true;synchronized(B) {
System.out.println("th2獲得了B的鎖");try{
Thread.sleep(1000);
}catch(Exception e) {
}
System.out.println("th2還想要A的鎖");synchronized(A) {
System.out.println("th2獲得了A的鎖");
}
}
}public voidrun(){if(flag) {
AccessA();
}else{
AccessB();
}
}
}
顯示結果:
程序沒有結束 而是停在了這里,這就是死鎖。
總結
以上是生活随笔為你收集整理的java 多线程跑数据_java——多线程的实现方式、三种办法解决线程赛跑、多线程数据同步(synchronized)、死锁...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员100套简历模板,全网最全
- 下一篇: android 版本号命名规范,软件版本