在开发中多线程并发问题,数据安全,很重要,
如果用了多线程就不的不考虑数据安全,
如何让主线程等待所有子线程都执行完,在继续执行?
方法很多,
1.在主方法中做现场数标记,如果有子线程没执行完就循环检测。
2.Thread.join()方法等待线程结束。
第一种方法:
- package com.yangtb.Thread;
-
- import java.util.Random;
-
- public class ThreadJoin {
- int intThreadNum;//记录子线程个数
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ThreadJoin tj = new ThreadJoin();
- tj.run();
- System.out.println("主线程结束");
- }
-
-
-
- class Work implements Runnable{
- String name;
- int sleeptime;
- Work(String name, int sleeptime){
- this.name = name;
- this.sleeptime=sleeptime;
- }
- public void run() {
-
- // TODO Auto-generated method stub
- try {
- //线程加1
- upThread();
- Thread.sleep(sleeptime);
- System.out.println(name+"---end---sleep :"+sleeptime);
-
- downThread();//线程减1
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
-
-
-
- public void run(){
-
- int time = 10000;
- Random r = new Random();
- Thread[] rs = new Thread[10];
- for(int i =0;i<10;i++){
-
- rs[i] =new Thread(new Work(i+"",r.nextInt(time)));
- rs[i].start();
- System.out.println(i+"--Start---");
- }
-
-
- //每隔1秒检查是否还有线程,如果有就等1秒在检查,如果没有就结束
- while(isok()){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- System.out.println("----end--");
- }
- //线程结束
- private synchronized void downThread(){
- intThreadNum--;
- }
- //线程开始
- private synchronized void upThread(){
- intThreadNum++;
- }
- //检查现场数
- private synchronized boolean isok(){
- return intThreadNum>0;
- }
-
- }
第二种方法:Thread.join();
- package com.yangtb.Thread;
-
- import java.util.Random;
-
- public class ThreadJoin {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ThreadJoin tj = new ThreadJoin();
- tj.run();
- System.out.println("主线程结束");
- }
-
-
-
- class Work implements Runnable{
- String name;
- int sleeptime;
- Work(String name, int sleeptime){
- this.name = name;
- this.sleeptime=sleeptime;
- }
- public void run() {
-
- // TODO Auto-generated method stub
- try {
-
- Thread.sleep(sleeptime);
- System.out.println(name+"---end---sleep :"+sleeptime);
-
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
-
-
-
- public void run(){
-
- int time = 10000;
- Random r = new Random();
- Thread[] rs = new Thread[10];
- for(int i =0;i<10;i++){
-
- rs[i] =new Thread(new Work(i+"",r.nextInt(time)));
- rs[i].start();
- System.out.println(i+"--Start---");
- }
-
- for(Thread rtemp : rs){
- try {
- rtemp.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- for(Thread rtemp : rs){
- try {
- rtemp.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- System.out.println("----end--");
- }
-
-
-
- }
- package com.yangtb.Thread;
- import java.util.Random;
- public class ThreadJoin {
- int intThreadNum;//记录子线程个数
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ThreadJoin tj = new ThreadJoin();
- tj.run();
- System.out.println("主线程结束");
- }
- class Work implements Runnable{
- String name;
- int sleeptime;
- Work(String name, int sleeptime){
- this.name = name;
- this.sleeptime=sleeptime;
- }
- public void run() {
- // TODO Auto-generated method stub
- try {
- //线程加1
- upThread();
- Thread.sleep(sleeptime);
- System.out.println(name+"---end---sleep :"+sleeptime);
- downThread();//线程减1
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void run(){
- int time = 10000;
- Random r = new Random();
- Thread[] rs = new Thread[10];
- for(int i =0;i<10;i++){
- rs[i] =new Thread(new Work(i+"",r.nextInt(time)));
- rs[i].start();
- System.out.println(i+"--Start---");
- }
- //每隔1秒检查是否还有线程,如果有就等1秒在检查,如果没有就结束
- while(isok()){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println("----end--");
- }
- //线程结束
- private synchronized void downThread(){
- intThreadNum--;
- }
- //线程开始
- private synchronized void upThread(){
- intThreadNum++;
- }
- //检查现场数
- private synchronized boolean isok(){
- return intThreadNum>0;
- }
- }
- package com.yangtb.Thread;
- import java.util.Random;
- public class ThreadJoin {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ThreadJoin tj = new ThreadJoin();
- tj.run();
- System.out.println("主线程结束");
- }
- class Work implements Runnable{
- String name;
- int sleeptime;
- Work(String name, int sleeptime){
- this.name = name;
- this.sleeptime=sleeptime;
- }
- public void run() {
- // TODO Auto-generated method stub
- try {
- Thread.sleep(sleeptime);
- System.out.println(name+"---end---sleep :"+sleeptime);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void run(){
- int time = 10000;
- Random r = new Random();
- Thread[] rs = new Thread[10];
- for(int i =0;i<10;i++){
- rs[i] =new Thread(new Work(i+"",r.nextInt(time)));
- rs[i].start();
- System.out.println(i+"--Start---");
- }
- for(Thread rtemp : rs){
- try {
- rtemp.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- for(Thread rtemp : rs){
- try {
- rtemp.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- System.out.println("----end--");
- }
- }