扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
首先,你同步的是具体的某个Test实例, 对于那个实例来说,实际上只有一个线程访问了那个代码块,但是sum和other却是多个线程同时去进行访问,实际上这是不安全的,如果你想实现每次都输出10000的效果,那么正确的应该是在Test.class上加锁,而不是获取Test实例的锁,修改后的代码如下:
网站设计制作过程拒绝使用模板建站;使用PHP+MYSQL原生开发可交付网站源代码;符合网站优化排名的后台管理系统;成都网站制作、成都做网站收费合理;免费进行网站备案等企业网站建设一条龙服务.我们是一家持续稳定运营了十余年的成都创新互联网站建设公司。
public class Test extends Thread {
public static int sum = 10000;
public static int other = 0;
public void getMoney() {
synchronized (Test.class) {
System.out.println(Thread.currentThread().getName() + " 开始执行");
sum = sum - 100;
System.out.println("sum-100");
other = other + 100;
System.out.println("other+100");
System.out.println(sum + other);
System.out.println(Thread.currentThread().getName() + " 执行完成");
}
}
public void run() {
getMoney();
}
public static void main(String[] agrs) {
Thread t[] = new Thread[10];
for (int i = 0; i = 9; i++) {
t[i] = new Test();
t[i].start();
}
}
}
// 上面代码能得到你的结果
照着下面的例子改一下:
/**
* 实现Runnable接口的类
*
* @author leizhimin 2008-9-13 18:12:10
*/
publicclass DoSomethingimplements Runnable {
private String name;
public DoSomething(String name) {
this.name = name;
}
publicvoid run() {
for (int i = 0; i 5; i++) {
for (long k = 0; k 100000000; k++) ;
System.out.println(name + ": " + i);
}
}
}
/**
* 测试Runnable类实现的多线程程序
*
* @author leizhimin 2008-9-13 18:15:02
*/
publicclass TestRunnable {
publicstaticvoid main(String[] args) {
DoSomething ds1 = new DoSomething("阿三");
DoSomething ds2 = new DoSomething("李四");
Thread t1 = new Thread(ds1);
Thread t2 = new Thread(ds2);
t1.start();
t2.start();
}
}
执行结果:
李四: 0
阿三: 0
李四: 1
阿三: 1
李四: 2
李四: 3
阿三: 2
李四: 4
阿三: 3
阿三: 4
Process finished with exit code 0
2、扩展Thread类实现的多线程例子
/**
* 测试扩展Thread类实现的多线程程序
*
* @author leizhimin 2008-9-13 18:22:13
*/
publicclass TestThreadextends Thread{
public TestThread(String name) {
super(name);
}
publicvoid run() {
for(int i = 0;i5;i++){
for(long k= 0; k 100000000;k++);
System.out.println(this.getName()+" :"+i);
}
}
publicstaticvoid main(String[] args) {
Thread t1 = new TestThread("阿三");
Thread t2 = new TestThread("李四");
t1.start();
t2.start();
}
}
执行结果:
阿三 :0
李四 :0
阿三 :1
李四 :1
阿三 :2
李四 :2
阿三 :3
阿三 :4
李四 :3
李四 :4
Process finished with exit code 0
package com.zhidao20161220;
public class Main {
private static int count=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i10;i++)
{
String name = "人员"+(i+1);
ManThread manThread = new ManThread(name);
manThread.start();
}
}
public static synchronized void go(String name)
{
count++;
System.out.println("线程"+Thread.currentThread().getName()+"执行,"+name+"正在通过山洞");
try {
if(count=10)
{
System.out.println("全员通过程序结束");
}
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.zhidao20161220;
public class ManThread extends Thread {
private String name;
public ManThread(String name)
{
this.name = name;
}
public void run() {
Main.go(name);
}
}
/*
class A extends Thread
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}
class B extends Thread
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}
class C extends Thread
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}
public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}*/
public class Test_1
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C c = new C();
Thread ta = new Thread(a);
Thread tb = new Thread(b);
Thread tc = new Thread(c);
ta.start();
tb.start();
tc.start();
}
}
class A implements Runnable
{
public void run()
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
System.out.println("A ERROR!");
}
System.out.println("AAAA");
}
}
class B implements Runnable
{
public void run()
{
try
{
Thread.sleep(2000);
}catch(Exception e)
{
System.out.println("B ERROR!");
}
System.out.println("BBBB");
}
}
class C implements Runnable
{
public void run()
{
try
{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println("C ERROR!");
}
System.out.println("CCCC");
}
}
案例一的两种方法已经写好;现在有事,晚上再把案例二代码写一下,应该没关系吧!
抱歉,是一个线程类,我看错了,晚上重发一下代码!
package know;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class T20 {
public static void main(String[] args) throws IOException {
Scanner s=new Scanner(System.in);
String username=null;
String password=null;
System.out.println("输入用户名:");
while(true){
if(username==null){
username=s.next();
System.out.println("输入密码:");
}else{
password=s.next();
}
if(password!=null){
s.close();
break;
}
}
BufferedReader br=null;
MapString, String map=new HashMapString, String();
try{
br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/test.txt")));
String temp=null;
while((temp=br.readLine())!=null){
String[] ss=temp.split("=");
map.put(ss[0], ss[1]);
}
}catch(IOException e){
throw e;
}finally{
if(br!=null)
br.close();
}
String u=map.get("userName");
String p=map.get("password");
if(u.equals(username)p.equals(password)){
System.out.println("登录成功");
}else{
System.out.println("用户名或密码错误");
}
}
}
package know;
public class T21 {
public static void main(String[] args) throws InterruptedException {
String[] persons=new String[]{"甲","乙","丙","丁","午","己","庚","辛","壬","癸"};
for(int i=0;ipersons.length;i++){
System.out.println(persons[i]+"正在过山洞");
Thread.sleep(5000);
}
}
}
最后一个百度搜一个就行了,肯定比我画的好
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流