Java 编写多线程的最佳方法
我必须做一个多线程作业。我有两门起重机运动课。 在这种情况下,有两个选项,无论爪超声波传感器是否看到盒子(见下图)。 在另一节课中,我必须从初始起始位置拿起一个盒子,然后将它从1-3放置到另一个位置(这是一个基于按键的距离) 我该如何写我错过的课程Java 编写多线程的最佳方法,java,robotics,nxt,Java,Robotics,Nxt,我必须做一个多线程作业。我有两门起重机运动课。 在这种情况下,有两个选项,无论爪超声波传感器是否看到盒子(见下图)。 在另一节课中,我必须从初始起始位置拿起一个盒子,然后将它从1-3放置到另一个位置(这是一个基于按键的距离) 我该如何写我错过的课程 public class Cases extends Thread UltrasonicSensor usc= new UltrasonicSensor(SensorPort.S1); int box=0; boolean isBoxThere=t
public class Cases extends Thread
UltrasonicSensor usc= new UltrasonicSensor(SensorPort.S1);
int box=0;
boolean isBoxThere=true;
public Cases cases;
public void error() throws InterruptedException
{
LCD.drawString("There are no boxes", 0, 0);
LCD.drawString(" At the location", 0, 1);
Button.waitForAnyPress();
LCD.clear();
Thread.sleep(1000);
}
//Claw movement
public void down() throws InterruptedException{
MotorPort motorY=MotorPort.B;
motorY.resetTachoCount();
motorY.controlMotor(85, 1);
stopY();
}
public void up() throws InterruptedException{
MotorPort motorY = MotorPort.B;
motorY.controlMotor(85, 2);
while(motorY.getTachoCount()>0); //After the crane grabs the box it lifts it back up
stopY();
}
public void clawclose() throws InterruptedException{
MotorPort motorC=MotorPort.C;
motorC.controlMotor(65, 2);
{
Thread.sleep(505); //Thread sleep is used for the cranes precise stopping
}
motorC.controlMotor(100, 3);
{
Thread.sleep(1000);
}
stopC();
}
public void clawopen() throws InterruptedException{
MotorPort motorC=MotorPort.C;
{
Thread.sleep(520);
}
motorC.controlMotor(100, 3);
{
Thread.sleep(1000);
}
stopC();
}
//Codes for stopping of Motors
public static void stopY () throws InterruptedException{
MotorPort motorY = MotorPort.B;
motorY.controlMotor(100, 3);
Thread.sleep(1000);
}
public static void stopC () throws InterruptedException{
MotorPort motorC = MotorPort.C;
motorC.controlMotor(100, 3);
Thread.sleep(1000);
}
public void run()
{
try
{
while(true)
{
switch(box)
{
case 1:
{
while(box==1)
{
isBoxThere=true;
try {
clawopen();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
down();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(usc.getDistance()<10)
{
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
try
{
clawclose();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
case 2:
{
while(box==0)
{
isBoxThere=false;
try
{
error();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
}
finally{}
}
公共类案例扩展线程
超声波传感器usc=新的超声波传感器(传感器端口S1);
int-box=0;
布尔值isBoxThere=true;
公共案件;
public void error()引发InterruptedException
{
拉丝(“没有盒子”,0,0);
LCD.拉丝(“在位置”,0,1);
按钮。waitForAnyPress();
LCD.clear();
睡眠(1000);
}
//爪运动
public void down()抛出InterruptedException{
MotorPort motorY=MotorPort.B;
motorY.resetTachoCount();
电机控制电机(85,1);
stopY();
}
public void up()引发InterruptedException{
MotorPort motorY=MotorPort.B;
电机控制电机(85,2);
while(motorY.getTachoCount()>0);//起重机抓起箱子后,它会将箱子抬起来
stopY();
}
public void clawclose()引发InterruptedException{
MotorPort motorC=MotorPort.C;
电机C.控制电机(65,2);
{
Thread.sleep(505);//线程睡眠用于起重机的精确停止
}
电机控制电机(100,3);
{
睡眠(1000);
}
stopC();
}
public void clawopen()引发InterruptedException{
MotorPort motorC=MotorPort.C;
{
睡眠(520);
}
电机控制电机(100,3);
{
睡眠(1000);
}
stopC();
}
//电机停止代码
publicstaticvoidstopy()抛出InterruptedException{
MotorPort motorY=MotorPort.B;
电机控制电机(100,3);
睡眠(1000);
}
publicstaticvoidstopc()抛出InterruptedException{
MotorPort motorC=MotorPort.C;
电机控制电机(100,3);
睡眠(1000);
}
公开募捐
{
尝试
{
while(true)
{
开关(箱)
{
案例1:
{
while(box==1)
{
isBoxThere=true;
试一试{
爪开();
}捕捉(中断异常e2){
//TODO自动生成的捕捉块
e2.printStackTrace();
}
试一试{
向下();
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
while(usc.getDistance())您需要调用this.start()
来启动线程。我必须把这个放在哪里。start?把它放在任何您想启动线程的地方。理想情况下,当您创建线程对象时。@killjoy下面是代码应该是什么样子的活动图