Java线程同步引用有很多的使用范围,在不断的使用中我们有很多的知识需要学习。我们可以在计算机上运行各种计算机软件程序。每一个运行的程序可能包括多个独立运行的线程。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、虚拟空间、营销软件、网站建设、泾源网站维护、网站推广。
- public class SynTest ...{
 - private String firstName, lastName;
 - private synchronized void setName(String firstName, String lastName) ...{
 - print("entering setName");
 - this.firstName = firstName;
 - print("Set first name have done firstName=" + this.firstName);
 - try ...{
 - Thread.sleep(1000);
 - } catch (InterruptedException e) ...{
 - }
 - this.lastName = lastName;
 - print("set last name have done,and leave setName() method.firstName="
 - + this.firstName + " lastName=" + this.lastName);
 - }
 - private void print(String msg) ...{
 - String thread = Thread.currentThread().getName();
 - System.out.println(thread + ": " + msg);
 - }
 - public static void main(String[] args) ...{
 - // 必需声明为final,否则runnable里面的run()方法不能访问。
 - final SynTest test1 = new SynTest();
 - final SynTest test2 = new SynTest();
 - Runnable run1 = new Runnable() ...{
 - public void run() ...{
 - test1.setName("arzu", "guli");
 - }
 - };
 - Thread threadOne = new Thread(run1, "threadOne");
 - threadOne.start();
 - try ...{
 - Thread.sleep(200);
 - } catch (InterruptedException e) ...{
 - }
 - Runnable run2 = new Runnable() ...{
 - public void run() ...{
 - // 如果这个线程引用的是对象test2,则setName方法不需要同步,也可以保证程序达到预期目的。
 - test1.setName("kang", "midi");
 - }
 - };
 - Thread threadTwo = new Thread(run2, "threadTwo");
 - threadTwo.start();
 - System.out.println("main() exit");
 - }
 - }
 
以上就是对Java线程同步引用的详细介绍。
                网页标题:Java线程同步引用基本代码介绍
                
                文章网址:http://www.csdahua.cn/qtweb/news22/95622.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网