关于线程,这个代码该怎么改?

YXTS122 2016-09-09 07:05:31
import java.util.ArrayList;
import java.util.List;



public class ThreadPriority
{
public static void main(String[]args)
{
List list = new ArrayList();
Student stu1 = new Student("张三", 130);
list.add(stu1);
Student stu2 = new Student("李四", 150);
list.add(stu2);
Student stu3 = new Student("王五", 120);
list.add(stu3);
Student stu4 = new Student("赵六", 140);
list.add(stu4);
Student stu5 = new Student("钱七", 170);
list.add(stu5);
new Score().RowSeat(list);
}
}
class Score extends Thread
{
Thread thread;
public Score(){}
public Score(String name)
{
thread=new Thread(this,name);
}
public void run()
{
System.out.println("一年一班的学生成员:"+thread.getName());
}
public void RowSeat(List list)
{
Student st=(Student)list.get(0);
Student st1=(Student)list.get(1);
Student st2=(Student)list.get(2);
Student st3=(Student)list.get(3);
Student st4=(Student)list.get(4);
Score e1=new Score(st.name);
Score e2=new Score(st1.name);
Score e3=new Score(st2.name);
Score e4=new Score(st3.name);
Score e5=new Score(st4.name);
e1.thread.setPriority(st.score/10);
e2.thread.setPriority(st1.score/10);
e3.thread.setPriority(st2.score/10);
e4.thread.setPriority(st3.score/10);
e5.thread.setPriority(st4.score/10);
e1.thread.start();
e2.thread.start();
e3.thread.start();
e4.thread.start();
e5.thread.start();
try
{
e5.thread.join();
}
catch (InterruptedException e)
{
System.out.println("等待线程结束出错:"+e.getMessage());
}
}
}
class Student
{
public String name;
public int score;
public Student(String name,int score)
{
this.name=name;
this.score=score;
}
}










...全文
185 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2016-09-10
  • 打赏
  • 举报
回复
import java.util.ArrayList;
import java.util.List;



public class ThreadPriority
{
	public static void main(String[]args)
	{
		List<Student> list = new ArrayList<Student>();
		Student stu1 = new Student("张三", 68);
		list.add(stu1);
		Student stu2 = new Student("李四", 85);
	    list.add(stu2);
		Student stu3 = new Student("王五", 70);
	    list.add(stu3);
		Student stu4 = new Student("赵六", 51);
    	list.add(stu4);
		Student stu5 = new Student("钱七", 98);
	    list.add(stu5);
		new Score().RowSeat(list);
	}
}
class Score extends Thread
{
	Thread thread;
	public Score(){}
	public Score(String name)
	{
		thread=new Thread(this,name);
	}
	public void run()
	{
		System.out.println("一年一班的学生成员:"+thread.getName());
	}
	public void RowSeat(List<Student> list)
	{
		Student st=(Student)list.get(0);
		Student st1=(Student)list.get(1);
		Student st2=(Student)list.get(2);
		Student st3=(Student)list.get(3);
		Student st4=(Student)list.get(4);
		Score e1=new Score(st.name);
		Score e2=new Score(st1.name);
		Score e3=new Score(st2.name);
		Score e4=new Score(st3.name);
		Score e5=new Score(st4.name);
		e1.thread.setPriority(st.score/10);
		e2.thread.setPriority(st1.score/10);
		e3.thread.setPriority(st2.score/10);
		e4.thread.setPriority(st3.score/10);
		e5.thread.setPriority(st4.score/10);
		e1.thread.start();
		e2.thread.start();
        e3.thread.start();
        e4.thread.start();
        e5.thread.start();
        try
        {
        	e5.thread.join();
        }
        catch (InterruptedException e)
        {
        	System.out.println("等待线程结束出错:"+e.getMessage());
        }
	}
}
class Student
{
	public String name;
	public int score;
	public Student(String name,int score)
	{
		this.name=name;
		this.score=score;
	}
}


		
		
		  
qq_36074845 2016-09-09
  • 打赏
  • 举报
回复
优先级是1-10吧,超过了
李大白白 2016-09-09
  • 打赏
  • 举报
回复
setPriority()这个方法是设置线程优先级的, 优先等级是 1~10 ,

        e1.thread.setPriority(st.score/10);
        e2.thread.setPriority(st1.score/10);
        e3.thread.setPriority(st2.score/10);
        e4.thread.setPriority(st3.score/10);
        e5.thread.setPriority(st4.score/10);
这几个都超过10了, 你尝试改为1~10以内试试

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧