一,java中使用Thread类实现多线程。
1,如果有两以上的线程同时访问同一个共享资源,可能造成线程冲突,线程冲突会造成数据丢失、重复等严重问题。
以下通过两个线程同时访问同一个类,来表现线程冲突,如果产生冲突便会打印输出。
例:
package Thread;public class Tested {String name;public static void main(String[] args) {Tested b=new Tested();Thread a=new Thread() {public void run() {try {while(true) {Thread.sleep(2000);b.aa("ls");}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};a.start();Thread e=new Thread() {public void run() {try {while(true) {Thread.sleep(2000);b.aa("aas");}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};e.start();}public void aa(String name) {this.name=name;eqrr(name);}public void eqrr(String name) {if(!(name==this.name)) {System.out.println(this.name+""+name);}}}
2,解决方法可以使用synchronized关键字让线程同步。
例:
package thinkmore;import java.util.Scanner;/*线程的冲突在多个线程访问同一个对象的同一个属性时,才会出现线程冲突,线程冲突会造成数据丢失、重复等严重问题。为了避免这个问题可以synchronized关键字让线程同步。*/public class Test {String name;public static void main(String[] args) {final Test obj=new Test();final Test obj2=new Test();final Thread t=new Thread(){@Overridepublic void run(){while (true){try {obj.setName("张三");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};t.start();final Thread t2=new Thread(){@Overridepublic void run(){while (true){try {obj2.setName("李四");Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};t2.start();}/*
1,放在函数中锁住整个函数,只有前一个线程执行完下一个函数才能访问
2,放在代码块中,锁住需要共享的资源,推荐使用
*/public /*synchronized*/ void setName(String name){// synchronized(this) {this.name = name;equalsName(name);// } }public void equalsName(String name){if(name!=this.name)System.out.println(name+" "+this.name);}}