线程模式——Immutable模式

线程模式

模式是对前人大量编码形式的总结,学习各种编程模式可以让我们更高效的进行项目编码

Immutable模式

在多线程的程序中,往往有存在一些公共资源,多个线程可以同时对该资源进行读取操作并且保证资源中的值不发生任何改变,这样的类我们就成为使用了Immutable模式的类。
如果该资源是可以被改变的,那么多线程在对资源执行写操作时就会发生不可预料的结果。

Java中已经实现immutable模式的类有 String 类,而StringBuffer正好是反例。
在这里插入图片描述

示例:

public class Person {//允许同时访问的共享资源类
    private final String name;
    private final String address;
    public Person(String n,String a){
        name=n;
        address=a;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String toString(){
        return "[person:name="+name+",address="+address+"]";
    }
}

public class WorkerThread extends Thread {//多线程访问者
    private Person person;
    public WorkerThread(Person p){
        person=p;
    }
    public void run(){
        while(true){
            System.out.println(Thread.currentThread().getName()+" prints "+person);
        }
    }//线程的访问并不会影响资源的状态,这里仅仅将其打印出来
}

public static void main(String[] args){
    System.out.println("Testing Immutable,hit CTRL+C exit!");
    Person p=new Person("Alice","Alaska");
    new WorkerThread(p).start();
    new WorkerThread(p).start();
    new WorkerThread(p).start();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.apache.http.ConnectionClosedException.class org.apache.http.ConnectionReuseStrategy.class org.apache.http.Consts.class org.apache.http.ContentTooLongException.class org.apache.http.ExceptionLogger.class org.apache.http.FormattedHeader.class org.apache.http.Header.class org.apache.http.HeaderElement.class org.apache.http.HeaderElementIterator.class org.apache.http.HeaderIterator.class org.apache.http.HttpClientConnection.class org.apache.http.HttpConnection.class org.apache.http.HttpConnectionFactory.class org.apache.http.HttpConnectionMetrics.class org.apache.http.HttpEntity.class org.apache.http.HttpEntityEnclosingRequest.class org.apache.http.HttpException.class org.apache.http.HttpHeaders.class org.apache.http.HttpHost.class org.apache.http.HttpInetConnection.class org.apache.http.HttpMessage.class org.apache.http.HttpRequest.class org.apache.http.HttpRequestFactory.class org.apache.http.HttpRequestInterceptor.class org.apache.http.HttpResponse.class org.apache.http.HttpResponseFactory.class org.apache.http.HttpResponseInterceptor.class org.apache.http.HttpServerConnection.class org.apache.http.HttpStatus.class org.apache.http.HttpVersion.class org.apache.http.MalformedChunkCodingException.class org.apache.http.MessageConstraintException.class org.apache.http.MethodNotSupportedException.class org.apache.http.NameValuePair.class org.apache.http.NoHttpResponseException.class org.apache.http.ParseException.class org.apache.http.ProtocolException.class org.apache.http.ProtocolVersion.class org.apache.http.ReasonPhraseCatalog.class org.apache.http.RequestLine.class org.apache.http.StatusLine.class org.apache.http.TokenIterator.class org.apache.http.TruncatedChunkException.class org.apache.http.UnsupportedHttpVersionException.class org.apache.http.annotation.Contract.class org.apache.http.annotation.Experimental.class org.apache.http.annotation.Obsolete.class org.apache.http.annotation.ThreadingBehavior.class org.apache.http.annotation.package-info.class org.apache.http.concurrent.BasicFuture.class org.apache.http.concurrent.Cancellable.class org.apache.http.concurrent.FutureCallback.class org.apache.http.concurrent.package-info.class org.apache.http.config.ConnectionConfig.class org.apache.http.config.Lookup.class org.apache.http.config.MessageConstraints.class org.apache.http.config.Registry.class org.apache.http.config.RegistryBuilder.class org.apache.http.config.SocketConfig.class org.apache.http.config.package-info.class org.apache.http.entity.AbstractHttpEntity.class org.apache.http.entity.BasicHttpEntity.class org.apache.http.entity.BufferedHttpEntity.class org.apache.http.entity.ByteArrayEntity.class org.apache.http.entity.ContentLengthStrategy.class org.apache.http.entity.ContentProducer.class org.apache.http.entity.ContentType.class org.apache.http.entity.EntityTemplate.class org.apache.http.entity.FileEntity.class org.apache.http.entity.HttpEntityWrapper.class org.apache.http.entity.InputStreamEntity.class org.apache.http.entity.SerializableEntity.class org.apache.http.entity.StringEntity.class org.apache.http.entity.package-info.class org.apache.http.impl.AbstractHttpClientConnection.class org.apache.http.impl.AbstractHttpServerConnection.class org.apache.http.impl.BHttpConnectionBase.class org.apache.http.impl.ConnSupport.class org.apache.http.impl.DefaultBHttpClientConnection.class org.apache.http.impl.DefaultBHttpClientConnectionFactory.class org.apache.http.impl.DefaultBHttpServerConnection.class org.apache.http.impl.DefaultBHttpServerConnectionFactory.class org.apache.http.impl.DefaultConnectionReuseStrategy.class org.apache.http.impl.DefaultHttpClientConnection.class org.apache.http.impl.DefaultHttpRequestFactory.class org.apache.http.impl.DefaultHttpResponseFactory.class org.apache.http.impl.DefaultHttpServerConnection.class org.apache.http.impl.EnglishReasonPhraseCatalog.class org.apache.http.impl.HttpConnectionMetricsImpl.class org.apache.http.impl.NoConnectionReuseStrategy.class org.apache.http.impl.SocketHttpClientConnection.class org.apache.http.impl.SocketHttpServerConnection.class org.apache.http.impl.bootstrap.HttpServer.class org.apache.http.impl.bootstrap.RequestListener.class org.apache.http.impl.bootstrap.SSLServerSetupHandler.class org.apache.http.impl.bootstrap.ServerBootstrap.class org.apache.http.impl.bootstrap.ThreadFactoryImpl.class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值