java程序中四个线程(两个自增10次两个自减10次),加了锁之后的输出结果为何没有连续自增或自减

java程序中四个线程(两个自增10次两个自减10次),加了锁之后的输出结果为何没有连续自增或自减,第1张

你锁加的不纯粗对,哪有锁这么用的。你既然是薯裤好要保护那个ThreadText.n,那锁也要数铅用一个啊,你相当于给这个东西上了四把锁,拿到锁的都能改。你把private Lock lock=new ReentrantLock()这句话抽成一个静态的,那四个线程都用这一个就行了

package

com.ufotable.test

abstract

class

Employee

{

String

name

String

birthday

double

money

void

setMoney(double

m){money=m}

Employee(String

name1,String

bir)

{name=name1birthday=bir}

abstract

double

getSalary(int

month)

}

class

SalariedEmployee

extends

Employee

{

SalariedEmployee(String

na,String

bir)

{super(na,bir)}

double

getSalary(int

month)

{

int

m=0

m=Integer.valueOf(birthday.substring(birthday.indexOf(".")+1,birthday.lastIndexOf(".")))

if(m==month)

return

money+100

else

return

money

}

}

/**ColaEmployee

的子类,按小时工资的员工,每月工作粗圆超出160

小时的部分按照1.5

倍工资发放。

money:每小时的工资、hours:每月工作的小时数*/

class

HourlyEmployee

extends

Employee{

HourlyEmployee(String

na,

String

bir)

{

super(na,

bir)

}

double

getSalary(int

hours){

return

money*hours+(hours>160?0.5*(hours-160)*money:0)

}

}

/**ColaEmployee

的子类,销售人员,工资由月销售额和提成率决定携伍。

money:月销售额、rates:提成率(80%就输入80)*/

class

SalesEmployee

extends

Employee{

SalesEmployee(String

name1,

String

bir)

{

super(name1,

bir)

}

double

getSalary(int

rates)

{

return

money*rates/100

}

}

class

Company{

static

void

print(Employee

e,int

i){

System.out.println("姓名:"+e.name+"\t月薪:"+e.getSalary(i))

}

}

public

class

Main

{

//这里的计算不是很科学,因为月份或者月工作小时或者提成率都是设定为了10,以下只是为了反映出多态

public

static

void

main(String[]

args)

{

Employee

a[]=new

Employee[10]

a[0]=new

SalariedEmployee("zhang1","1980.5.10")

a[1]=new

SalariedEmployee("zhang2","1990.10.10")

a[2]=new

HourlyEmployee("zhang4","1910.10.10")

a[3]=new

HourlyEmployee("zhang5","1960.10.10")

a[4]=new

HourlyEmployee("zhang6","1940.10.10")

a[5]=new

SalesEmployee("zhang7"辩凳或,"1920.10.10")

a[6]=new

SalesEmployee("zhang8","1970.10.10")

a[7]=new

SalesEmployee("zhang9","1990.10.10")

a[8]=new

SalesEmployee("zhang3","1990.10.10")

a[9]=new

SalesEmployee("zhang10","1900.10.10")

for(int

i=0i<a.lengthi++)

a[i].setMoney(3000.0)

System.out.println("打印10月份员工的工资:")

for(int

i=0i<a.lengthi++)

Company.print(a[i],

10)

}

}

import java.text.DecimalFormat

import java.util.Scanner

public class Zhidao {

public static void main(String[] args) {

String condition = ""

Zhidao zhidao = new Zhidao()

do{

Scanner scanner = new Scanner(System.in)

try{

System.out.print("请输入第一个数:")

double x = scanner.nextDouble()

System.out.print("请输入第二个数:")

double y = scanner.nextDouble()

System.out.print("请输入配如隐运算符:")

String s = scanner.next()

char z = s.charAt(0)

zhidao.yunsuan(x, y, z)

}catch(Exception e){

System.out.println("请输入正确的数据!")

}

System.out.print("是否继续?continue:继续,任意字符:结束")

condition = scanner.next()

}while("continue".equals(condition))

}

public static void yunsuan(double x,double y,Character z){

DecimalFormat r=new DecimalFormat()

r.applyPattern("#0.00")

if(z.equals('+')){

System.out.println(x+"+"+y+"=" + r.format((x+y)))

} else if(z.equals('-')){

System.out.println(x+"-"培厅+y+"=" + r.format((x-y)))

} else if(z.equals('*')){

System.out.println(x+"*"+y+"=" + r.format((x*y)))

} else if(z.equals('/')){

if(y==0){

System.out.println("被橡拍除数不能为0")

} else{

System.out.println(x+"/"+y+"=" + r.format((x/y)))

}

}else{

System.out.println("无法识别改运算符")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/12473716.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-24
下一篇2023-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存