Java 线程开始运行并自行终止
更新:谢谢大家!我已经根据建议修改了程序,下面给出的代码就是修改后的代码Java 线程开始运行并自行终止,java,multithreading,Java,Multithreading,更新:谢谢大家!我已经根据建议修改了程序,下面给出的代码就是修改后的代码 原创帖子: 我已经通过了一些“应用和分析”类型的问题,在一个问题中,程序员被要求对电影院的三个预订柜台应用多线程概念,并计算一场演出的总预订数量和收集的金额 我已经为此编写了一个程序,您可以在下面看到: import java.io.*; import java.lang.*; class Cinema { int no=0,price=0; synchronized void reservation(
原创帖子: 我已经通过了一些“应用和分析”类型的问题,在一个问题中,程序员被要求对电影院的三个预订柜台应用多线程概念,并计算一场演出的总预订数量和收集的金额 我已经为此编写了一个程序,您可以在下面看到:
import java.io.*;
import java.lang.*;
class Cinema
{
int no=0,price=0;
synchronized void reservation(int n,int p)
{
no=no+n;
price=price+p;
}
}
class Counter implements Runnable
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Cinema c;
int not,cost;
Counter(Cinema c)
{
this.c=c;
}
public void run()
{
try
{
System.out.print("\nCounter 1");
System.out.print("\nEnter the no. of tickets :");
not=Integer.parseInt(br.readLine());
cost=not*150;
c.reservation(not,cost);
}
catch(IOException e){System.out.print("\n"+e);}
}
}
class CinemaMain
{
public static void main(String args[])throws IOException
{
Cinema c=new Cinema();
System.out.print("\nCounter 1");
Thread c1=new Thread(new Counter(c));
c1.start();
c1.join();
System.out.print("\nCounter 2");
Thread c2=new Thread(new Counter(c));
c2.start();
c2.join();
System.out.print("\nCounter 3");
Thread c3=new Thread(new Counter(c));
c3.start();
c3.join();
try
{
Thread.sleep(500);
}
catch(InterruptedException ie)
{
System.out.print("\n"+ie);
}
System.out.print("\nTotal no. of tickets :"+c.no);
System.out.print("\nTotal Money collected:"+c.price);
}
}
我可以很好地编译它,但是当我运行程序时,我得到的是-->(因为我没有10个声誉,所以我不能在这里发布图像,对不起!)我不知道为什么,它不要求输入,即使我已经编写了代码来在run方法中获取输入
我可以很好地编译它,但当我运行程序时,我得到的是
您的程序有两个问题:
计数器
线程完成。如果需要等待某个线程完成,则对其调用thread.join()
Thread counter1 = new Thread(new Counter1(c));
counter1.start();
// start other threads here...
// now wait for the counter1 to finish
counter1.join();
在您的例子中,3个计数器是分叉的,但主计数器只休眠一点,然后退出。计数器
线程仍在运行
计数器
线程都在向电影院
内的字段添加值,但是电影院
中没有同步。每当两个线程修改同一字段时,必须有一些互斥保护和内存同步
这里要做的简单的事情是将电影院预订(…)
方法同步化
。然后,每个计数器
对象将在电影院
实例上获得一个锁,这将确保一次只有一个计数器
更新电影院
。synchronized
关键字还确保Cinema
对象中的字段也是内存同步的
synchronized void reservation(int n,int p) { ...
既然可以简化线程,为什么还要创建线程呢
import java.io.*;
import java.lang.*;
import java.util.Scanner;
public class Cinema {
public Cinema(){
int no=0,price=0;
}
public int Count () {
int not,not2, not3, cost,cost2,cost3;
System.out.print("\nCounter 1");
System.out.print("\nEnter the no. of tickets: ");
Scanner br=new Scanner(System.in);
String input=br.nextLine();
not=Integer.parseInt(input);
cost=not*150;
System.out.println("Cost of tickets: "+cost);
System.out.print("\nCounter 2");
System.out.print("\nEnter the no. of tickets: ");
Scanner br2=new Scanner(System.in);
String input2=br2.nextLine();
not2=Integer.parseInt(input2);
cost2=not2*150;
System.out.println("Cost of tickets: "+cost2);
System.out.print("\nCounter 3");
System.out.print("\nEnter the no. of tickets: ");
Scanner br3=new Scanner(System.in);
String input3=br3.nextLine();
not3=Integer.parseInt(input3);
cost3=not3*150;
System.out.println("Cost of tickets: "+cost3);
int total=cost+cost2+cost3;
int tickets=not+not2+not3;
System.out.println("Total price for tickets is: "+total);
System.out.println("Total number of tickets is: "+tickets);
return total;
}
public static void main(String args[])
{
Cinema c=new Cinema();
c.Count();
}
}
尝试这种方法
import java.io.*;
import java.lang.*;
public class Cinema
{
int no=0,price=0;
synchronized void reservation(int n,int p)
{
no=no+n;
price=price+p;
}
public static void main(String args[])throws IOException, InterruptedException
{
Cinema c=new Cinema();
Thread t1 = new Thread(new Counter(c,"Counter 1"));
t1.start();
Thread t2 = new Thread(new Counter(c,"Counter 2"));
t2.start();
Thread t3 = new Thread(new Counter(c,"Counter 3"));
t3.start();
t1.join();
t2.join();
t3.join();
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.print("\n"+ie);
}
System.out.print("\nTotal no. of tickets :"+c.no);
System.out.print("\nTotal Money collected:"+c.price);
}
}
class Counter implements Runnable
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Cinema c;
int not,cost;
String counterName;
Counter(Cinema c,String counterName)
{
this.c=c;
this.counterName=counterName;
}
public void run()
{
try
{
synchronized(c) {
System.out.print("\n" + counterName);
System.out.print("\nEnter the no. of tickets :");
not=Integer.parseInt(br.readLine());
cost=not*150;
c.reservation(not,cost);
}
}
catch(IOException e){System.out.print("\n"+e);}
}
}
考虑使用<代码>扫描器控制台输入。为什么需要3个线程?这个疯狂是什么?为什么不能让一个计数器类有三个实例呢?请注意,
run()
方法运行一次,然后线程完成并关闭。你的每个计数器类(都是同一个类,为什么要复制/粘贴三次?)。。。真的很有帮助!由于我是Java新手,所有这些线程和applet概念对我来说都有点困难,但像你这样的人真的很有帮助,再次感谢你的解释:)谢谢!但我已经更新过了。不管怎样,再次感谢你试图帮助我!:)