package 对象及变量的并发访问2;
import org.omg.PortableInterceptor.SUCCESSFUL;
import 多线程技能1.Run2;
import java.nio.channels.FileChannel;
import java.util.WeakHashMap;
/**
* 可见性的测试
* 关键字volatile具有可见性,可见性是指A线程更改变量后,B线程马上能够看到更改后的
*数据,提高程序灵敏度。
*
* 线程停止不了的主要原因是main线程值在处理while()循环,导致程序不能继续执行后面的代码。
*/
class PrintStringT2310{
private boolean isContinuePrint =true;
public boolean isContinuePrint(){
return isContinuePrint;
}
public void setContinuePrint(boolean isContinuePrint){
this.isContinuePrint =isContinuePrint;
}
public void PrintStringMethod(){
try {
while (isContinuePrint==true){
System.out.println("run printStringMethod threadName="+Thread.currentThread().getName());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class RunT2310{
public RunT2310(){
PrintStringT2310 printStringT2310=new PrintStringT2310();
printStringT2310.PrintStringMethod();
System.out.println("停止"+Thread.currentThread().getName());
printStringT2310.setContinuePrint(false);
}
}
/**
* 2.使用多线程解决死循环
*/
class PrintStrig2T2310 implements Runnable{
private boolean isContinuePrint=true;
public boolean isContinuePrint(){
return isContinuePrint;
}
public void setContinuePrint(boolean continuePrint) {
isContinuePrint = continuePrint;
}
public void printStringMethod(){
try {
while (isContinuePrint==true){
System.out.println("run printStringMethod Name="+Thread.currentThread().getName());
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
printStringMethod();
}
}
class Run2T2310{
public Run2T2310(){
PrintStrig2T2310 printStrig2T2310=new PrintStrig2T2310();
new Thread(printStrig2T2310).start();
System.out.println("停止"+Thread.currentThread().getName());
printStrig2T2310.setContinuePrint(false);
}
}
/**
* 3.使用多线程可能出现的死循环
*/
class RunThread extends Thread{
private boolean isRunning =true;
public boolean isRunning(){
return isRunning;
}
public void setRunning(boolean running) {
isRunning = running;
}
@Override
public void run() {
System.out.println("进入run");
while (isRunning==true){
}
System.out.println("停止了");
}
}
class Run3T2310{
public Run3T2310(){
try {
RunThread runThread=new RunThread();
runThread.start();
Thread.sleep(1000);
System.out.println("赋值false");
runThread.setRunning(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 4.使用volatile关键字解决多线程出现的死循环
* 造成死循环的原因:在启动RunThread.java线程时,变量“print boolean isRunning =true;"存在
*与公共堆栈及线程的私有堆栈中,线程运行后一直在线程的私有堆栈中取得的isRunning的值是true,而代码“
* thread.setRunning(false);"虽然被执行,更新的却是公共堆栈中的isRunning变量,改为了false,操作
* 的是两块内存地址中的数据,所以线程一直处于死循环状态
*
*/
class RunThread4 extends Thread{
volatile private boolean isRunning =true;
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean running) {
isRunning = running;
}
@Override
public void run() {
System.out.println("进入");
while (isRunning==true){
}
System.out.println("停止了");
}
}
class Run4T2310{
public Run4T2310(){
try {
RunThread4 runThread4=new RunThread4();
runThread4.start();
Thread.sleep(1000);
System.out.println("赋值false");
runThread4.setRunning(false);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* 5。synchronized代码块具有增加可见性的作用
* 关键字synchronized可以使用多线程访问同一个资源具有同步性,而且具有使用线程
*工作内存中的私有变量与共有内存中的变量同步的特性,即可见性。
*/
class Service5T2310{
private boolean isContinueRun=true;
public void runMethod(){
while (isContinueRun==true){
}
System.out.println("停止");
}
public void stopMethod(){
isContinueRun=false;
}
}
class MyThreadAT2310 extends Thread{
private Service5T2310 service5T2310;
public MyThreadAT2310(Service5T2310 service5T2310){
super();
this.service5T2310=service5T2310;
}
@Override
public void run() {
service5T2310.runMethod();
}
}
class MyThreadBT2310 extends Thread{
private Service5T2310 service5T2310;
public MyThreadBT2310(Service5T2310 service5T2310){
super();
this.service5T2310=service5T2310;
}
@Override
public void run() {
service5T2310.stopMethod();
}
}
class Run5T2310{
public Run5T2310(){
try {
Service5T2310 service5T2310=new Service5T2310();
MyThreadAT2310 at2310=new MyThreadAT2310(service5T2310);
at2310.start();
Thread.sleep(1000);
MyThreadBT2310 bt2310=new MyThreadBT2310(service5T2310);
bt2310.start();
System.out.println("发出停止");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Service52T2310{
private boolean isContinueRun=true;
public void runMethod(){
String anyString =new String();
while (isContinueRun==true){
synchronized (anyString){
}
}
System.out.println("停了");
}
public void stopMethod(){
isContinueRun= false;
}
}
class MyThreadA5T2310 extends Thread{
private Service52T2310 service52T2310;
public MyThreadA5T2310 (Service52T2310 service52T2310){
super();
this.service52T2310=service52T2310;
}
@Override
public void run() {
service52T2310.runMethod();
}
}
class MyThreadB5T2310 extends Thread{
private Service52T2310 service52T2310;
public MyThreadB5T2310(Service52T2310 service52T2310){
super();
this.service52T2310=service52T2310;
}
@Override
public void run() {
service52T2310.stopMethod();
}
}
class Run52T2310{
public Run52T2310() throws InterruptedException {
Service52T2310 service52T2310=new Service52T2310();
MyThreadA5T2310 a5T2310=new MyThreadA5T2310(service52T2310);
a5T2310.start();
Thread.sleep(1000);
MyThreadB5T2310 b5T2310=new MyThreadB5T2310(service52T2310);
b5T2310.start();
System.out.println("发出停止");
}
}
public class T2310 {
public static void main(String[] args) throws InterruptedException {
RunT2310 runT2310=new RunT2310();
//Run2T2310 run2T2310=new Run2T2310();
//Run3T2310 run3T2310=new Run3T2310();
//Run4T2310 run4T2310=new Run4T2310();
//Run5T2310 run5T2310 =new Run5T2310();
//Run52T2310 run52T2310=new Run52T2310();
}
}
Run52T2310 run52T2310=new Run52T2310();